所以,我只是在python中鬼混,我有一点错误。该脚本应该要求1,2或3.我的问题是,当用户输入1,2或3以外的东西时,我会崩溃。比如,如果用户输入4或ROTFLOLMFAO,它就会崩溃。
编辑:好的,将其切换为int(input())。还有问题 这是代码
#IMPORTS
import time
#VARIABLES
current = 1
running = True
string = ""
next = 0
#FUNCTIONS
#MAIN GAME
print("THIS IS A GAME BY LIAM WALTERS. THE NAME OF THIS GAME IS BROTHER")
#while running == True:
if current == 1:
next = 0
time.sleep(0.5)
print("You wake up.")
time.sleep(0.5)
print("")
print("1) Go back to sleep")
print("2) Get out of bed")
print("3) Smash alarm clock")
while next == 0:
next = int(input())
if next == 1:
current = 2
elif next == 2:
current = 3
elif next == 3:
current = 4
else:
print("invalid input")
next = 0
答案 0 :(得分:0)
使用raw_input()
而不是input()
后者eval将输入作为代码。
也许只是建立一个问函数
def ask(question, choices):
print(question)
for k, v in choices.items():
print(str(k)+') '+str(v))
a = None
while a not in choices:
a = raw_input("Choose: ")
return a
未经测试
答案 1 :(得分:0)
因为input()给你字符串值而next是一个整数,所以可能是因为那次冲突而发生崩溃的情况。尝试next = int(input()),我希望它适合你:)