2017-03-18 4 views
-1

저는이 파이썬 코드를 약 한 시간 동안 작동 시키려고 노력해 왔지만 그것을 고칠 수는 없습니다. 전에 파이썬에 들어갔다. 그렇다면 이것이 쉬운 이유가 바로 그 이유이다. 파이썬 텍스트 어드벤처 게임이 작동하지 않습니다.

def firstChoice(): 
    time.sleep(2) 
    print('You come across a path, it splits at the end.') 
    time.sleep(1) 
    choice=input('Which path do you take, the left path (1) or the right path (2)? \n') 
    checkChoice() 

def checkChoice(): 
# correct 
    if choice=='1' or choice=='2': 
     correct_choice=randint(1,2) 
     if choice==correct_choice: 
      correct=True 
    if choice!='1' or choice!='2': 
     print('You decide to not take a path, and you die due to random circumstances.') 
     time.sleep(1) 
     print('Take a path next time, or at least take it correctly.') 
     failScreen() 

I've imported everything necessary (time and random)

EDIT: Here's the whole code.

import random 
import time 

choice=0 

def introDisplay(): 
    print('This is the pre-game story.') 
    time.sleep(1) 
    print('It lasts for 5 lines.') 
    time.sleep(1) 
    print('When you can be arsed, fix this.') 
    time.sleep(1) 
    print('Thanks,') 
    time.sleep(1) 
    print('You, from 18/3/17') 
    print() 
    firstChoice() 

def firstChoice(): 
    time.sleep(2) 
    print('You come across a path, it splits at the end.') 
    time.sleep(1) 
    choice=input('Which path do you take, the left path (1) or the right path (2)? \n') 
    checkChoice(choice) 

def checkChoice(choice): 
    correct=False 
    if choice=='1' or choice=='2': 
     correct_choice=random.randint(1,2) 
     if choice==correct_choice: 
      correct=True 
    if choice!='1' and choice!='2': 
     print('You decide to not take a path, and you die due to random circumstances.') 
     time.sleep(1) 
     print('Take a path next time, or at least take it correctly.') 
     failScreen() 




def failScreen(): 
    restart=True 
    print('You have failed.') 
    print('Do you want to retry?') 
    restart1=input('Y or y = Yes. N or n = No. \n') 
    if restart1=='y' or restart1=='Y': 
     restart=True 
    if restart1=='n' or restart1=='N': 
     restart=False 
    if restart1!='n' or restart!='N' or restart!='y' or restart!='Y': 
     failScreen() 
    if restart==True: 
     introDisplay() 
    if restart==False: 
     exit() 

introDisplay() 
+0

'데프 checkChoice (선택) :'그리고 당신이 그것을'checkChoice (choice)'라고 부를 때. –

+0

작동하지 않는 경우에도 두 번째에 'else'를 사용해야합니다 ... – MobyCoding

+2

사이트 규칙에 따라 더 나은 (답변 할 수있는) 질문을 작성하는 방법은 http://stackoverflow.com/help/mcve를 참조하십시오. . 또한 http://stackoverflow.com/help/how-to-ask를 참고하십시오. 문제가 발생할 때 어떤 종류의 프로그램을 쓰는 것이 아닌, 문제 자체를 신경 써야합니다. 같은 문제가있는 ** 가능한 가장 짧은 프로그램 ** - 가장 짧은 프로그램은 아마도 텍스트 모험이 아닐 것입니다. 귀하의 질문이 귀하가 작성한 프로그램에 초점을 맞춘 것이지 문제가 아니라 잘못된 장소에 집중되어 있습니다. –

답변

0

우선 python2.7을 사용하는 경우 콘솔에서 숫자를 입력하면 input()을 알아야합니다. 함수를 int로 변환 한 다음 choice == 1처럼 값을 확인하거나 raw_input() (가장 좋은 방법)으로 입력을 변경하거나 if choice!='1' and choice!='2':에 넣으려는 경우 else:을 입력해야 많은 수표 또는 예상치 못한 값을 피할 수 있습니다.

당신은 python3의 raw_input을를 사용하는 경우는 새로운 입력 기능 그래서, 당신은 당신이 (raw_input을 사용하려면) 또는 python3를 사용하는 경우

이 그리고, 마지막으로 당신을 변경해야 변경할 필요가 없습니다 것입니다 당신은 INT와 STR을 비교하고 있기 때문에 random.choice('1','2') 랜덤 기능, 여기 당신이와 코드가 python2.7에 대한 변경이 : 당신은 함수에 인수로 검색을 통과해야

import random 
import time 

choice=0 

def introDisplay(): 
    print('This is the pre-game story.') 
    time.sleep(1) 
    print('It lasts for 5 lines.') 
    time.sleep(1) 
    print('When you can be arsed, fix this.') 
    time.sleep(1) 
    print('Thanks,') 
    time.sleep(1) 
    print('You, from 18/3/17') 
    print() 
    firstChoice() 

def firstChoice(): 
    time.sleep(2) 
    print('You come across a path, it splits at the end.') 
    time.sleep(1) 
    choice=raw_input('Which path do you take, the left path (1) or the right path (2)? \n') 
    checkChoice(choice) 

def checkChoice(choice): 
    correct=False 
    if choice=='1' or choice=='2': 
     correct_choice=random.choice(['1','2']) 
     if choice==correct_choice: 
      correct=True 
      print('You chose the correct path') 
     else: 
      print('You dont chose the correct path') 
    else: 
     print('You decide to not take a path, and you die due to random circumstances.') 
     time.sleep(1) 
     print('Take a path next time, or at least take it correctly.') 
     failScreen() 




def failScreen(): 
    restart=True 
    print('You have failed.') 
    print('Do you want to retry?') 
    restart1=input('Y or y = Yes. N or n = No. \n') 
    if restart1=='y' or restart1=='Y': 
     restart=True 
    if restart1=='n' or restart1=='N': 
     restart=False 
    if restart1!='n' or restart!='N' or restart!='y' or restart!='Y': 
     failScreen() 
    if restart==True: 
     introDisplay() 
    if restart==False: 
     exit() 

introDisplay() 
+0

이 모든 것은 작동하지만 raw_input은 오류를 일으켰지 만 모든 것을 입력하도록 변경하면 오류가 발생합니다. – MobyCoding

0

I guess you are always ending up in the failScreen 두 번째 if 문이 !=1 or !=2을 사용하고 있기 때문에 항상 true이됩니다 ... 도움이되는지 확인하려면 and으로 변경하십시오.

checkChoice 함수에 choice이 표시되는지 확실하지 않습니다. Java에서 함수 -bodys 외부의 변수를 선언해야합니다.

관련 문제