我一直在試圖讓這個Python代碼工作大約一個小時,但我似乎無法修復它。我前幾天進入了Python,所以如果這很容易,那就是原因。 Python文本冒險遊戲不工作
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()
您需要通過選擇作爲參數傳遞給函數'高清checkChoice(選擇):'當你把它叫做'checkChoice(choice)'時。如果 –
不起作用... – MobyCoding
請參閱http://stackoverflow.com/help/mcve關於根據網站規則構建更好(更多可回答)問題的指導。另請參閱http://stackoverflow.com/help/how-to-ask - 請注意,我們關心*問題本身*,而不是您遇到問題時編寫的程序類型(並且我們要求**最短的程序有相同的問題** - 最短的程序可能*不會是文本冒險)。如果你的問題關注於你正在寫的程序,而不是你正在遇到的問題,那麼它就集中在錯誤的地方。 –