Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python--在尝试处理意外输入时崩溃_Python - Fatal编程技术网

python--在尝试处理意外输入时崩溃

python--在尝试处理意外输入时崩溃,python,Python,所以,我只是在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

所以,我只是在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
使用
raw\u input()
而不是
input()
后一种评估方法将输入作为代码

也可能只是构建一个ask函数

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

但未测试,因为input()为您提供字符串值,next为整数,因此可能会因为该冲突而导致崩溃。试试next=int(input()),我希望它对您有用:)

您能发布完整的stacktrace吗?我在python 2.7上测试过,用原始输入替换输入。它不会崩溃,但总是发布无效的输入。(因为下一个是str not int)@Herrington-Darkholme:对不起,python还不太熟悉。我试过了,但是我怎样才能得到下一个int?它不是已经被设置为最顶端的整数了吗?
next=int(input())
。这会将str转换为INT,但仍有一些问题,请更新第一篇文章:/