我正在制作一个简单的游戏程序,但是在游戏开始时遇到了麻烦。我设置了三个功能:开始,游戏,结束。问题是,一旦程序创建了函数,它就会终止,因为没有重新启动来再次通过这些函数。
这是最后一个函数,gameEnd:
def gameEnd ( ):
print ('Would you like to play again? Y/N.')
playAgain = '' **part of the problem is here; the computer reads playAgain as **
input (playAgain) **''. Because playAgain is not equal to Y or y, the program **
if playAgain == 'Y' or 'y': **exits without ever starting. I need to move **
gameCore **playAgain somewhere logical.**
else:
print ('You won Caves of Doom, Caves of Delight ' + wins + ' times.')
print ('You lost Caves of Doom, Caves of Delight ' + losses + ' times.')
if wins > losses:
print ('Good for you. You have a winning record!')
elif wins == losses:
print ('Well, you did okay; neither good nor bad.')
elif wins < losses:
print ('Tough luck. You have a losing record.')
time.sleep (1)
print ('Farewell, ' + name + ',' + ' and may we meet again sometime soon.')
答案 0 :(得分:1)
通过查看代码,我认为您使用的是Python 3.如果您使用的是Python 2,那么请使用raw_input()
代替input()
。
你的错误是你定义变量的时候。如果您从用户那里获取输入,则编写变量,然后将等号放入符号,然后写入raw_input()
或input()
。
你就是这样做的:
variable = input() #In python 3
variable = raw_input() #In python 2
所以试试这段代码:
def gameEnd():
print('Would you like to play again? Y/N.')
playAgain = input("> ")
if playAgain == 'Y' or playAgain == 'y':
gameCore()
else:
print('You won Caves of Doom, Caves of Delight ' + wins + ' times.')
print('You lost Caves of Doom, Caves of Delight ' + losses + ' times.')
if wins > losses:
print('Good for you. You have a winning record!')
elif wins == losses:
print('Well, you did okay; neither good nor bad.')
elif wins < losses:
print('Tough luck. You have a losing record.')
time.sleep(1)
print('Farewell, ' + name + ',' + ' and may we meet again sometime soon.')
#End of code
答案 1 :(得分:0)
在gameEnd()上试用此版本。如果用户输入“Y”/“y”以外的任何内容,则退出。
def gameEnd ( ):
playAgain = raw_input ('Would you like to play again?')
if playAgain == 'Y' or playAgain == 'y':
gameCore()
else:
print ('You won Caves of Doom, Caves of Delight ' + wins + ' times.')
print ('You lost Caves of Doom, Caves of Delight ' + losses + ' times.')
if wins > losses:
print ('Good for you. You have a winning record!')
elif wins == losses:
print ('Well, you did okay; neither good nor bad.')
elif wins < losses:
print ('Tough luck. You have a losing record.')
time.sleep (1)
print ('Farewell, ' + name + ',' + ' and may we meet again sometime soon.')
答案 2 :(得分:0)
此处的输入功能被滥用。使用'input'时,Python解释器会尝试评估输入内容。这就是使用raw_input发回字符串更安全的原因。您可以使用以下代码调整代码的开头:
def gameEnd():
playAgain = raw_input('Play again ? (Y/y)')
if any([playInput == 'Y', playInput == 'y']):
gameCore()