Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Loops - Fatal编程技术网

Python如何使用用户输入停止循环

Python如何使用用户输入停止循环,python,loops,Python,Loops,这可能是一个简单的答案,但我想我还是会问 我下面的代码要求用户输入一个数字,并根据提供的答案打印与数字对应的分数 我想通过让用户输入(999)来停止循环(终止程序)。我知道问题出在我的中,如果userScore>=90“打印('A'),那么当用户输入999时,计算机将其视为A 有没有捷径可以解决这个问题 (PS我在每一行中添加了中断,因为当它们不在那里时,输出会不断重复。) userScore=float(输入('输入分数或键入“999”退出:')) 尽管如此: 尝试: 如果userScore>

这可能是一个简单的答案,但我想我还是会问

我下面的代码要求用户输入一个数字,并根据提供的答案打印与数字对应的分数

我想通过让用户输入(999)来停止循环(终止程序)。我知道问题出在我的
中,如果userScore>=90“打印('A')
,那么当用户输入999时,计算机将其视为A

有没有捷径可以解决这个问题

(PS我在每一行中添加了中断,因为当它们不在那里时,输出会不断重复。)

userScore=float(输入('输入分数或键入“999”退出:'))
尽管如此:
尝试:
如果userScore>=90:
打印(“您获得A”)
打破
elif userScore>=80:
打印(“你得了B”)
打破
elif userScore>=70:
打印(“你得了C”)
打破
elif userScore>=60:
打印(“你得了D”)
打破

elif userScore不要使用try-except。try-except用于错误处理。这可以使用一个简单的while循环来处理

userScore = float(input('Enter the score or type "999" to quit: '))

while userScore!=999:
    if userScore >= 90:
        print ("You earned an A")
        break

    elif userScore >= 80:
        print ("You earned a B")
        break

    elif userScore >= 70:
        print ("You earned a C")
        break

    elif userScore >= 60:
        print ("You earned a D")
        break

    elif userScore <= 59.9:
        print ("You earned an F")
        break
main()  # Why is this even required?
userScore=float(输入('输入分数或键入“999”退出:'))
当userScore!=999时:
如果userScore>=90:
打印(“您获得A”)
打破
elif userScore>=80:
打印(“你得了B”)
打破
elif userScore>=70:
打印(“你得了C”)
打破
elif userScore>=60:
打印(“你得了D”)
打破

elif userScore以下是您试图实现的目标。评论中对此进行了解释

while True:

    #This part gets the user input.  It waits until the user enters a valid number input.
    while True:
        prelim = input('Enter the score or type "999" to quit: ')
        try:
            prelim = int(prelim)
        except:
            print("Please enter a valid input.")
        else:
            #if the input can be converted into a number, then this is our final input value
            userScore = float(prelim)
            break

    #The first thing we should check is if the user wants to exit.  This way it won't print out an answer then exit.
    if userScore == 999:
        break

    if userScore >= 90:
        print ("You earned an A")


    elif userScore >= 80:
        print ("You earned a B")


    elif userScore >= 70:
        print ("You earned a C")


    elif userScore >= 60:
        print ("You earned a D")


    elif userScore <= 59.9:
        print ("You earned an F")
为True时:
#此部分获取用户输入。它等待用户输入有效的数字。
尽管如此:
prelim=input('输入分数或键入“999”退出:')
尝试:
prelim=int(prelim)
除:
打印(“请输入有效的输入。”)
其他:
#如果输入可以转换成一个数字,那么这就是我们的最终输入值
userScore=浮动(预调)
打破
#我们应该检查的第一件事是用户是否想要退出。这样它就不会打印出答案然后退出。
如果userScore==999:
打破
如果userScore>=90:
打印(“您获得A”)
elif userScore>=80:
打印(“你得了B”)
elif userScore>=70:
打印(“你得了C”)
elif userScore>=60:
打印(“你得了D”)

elif userScore只需先检查
userScore==“999”
;就在
try
之后。顺便问一下,为什么
try
try
通常用于错误检查-我不认为
try
是这里使用的正确语法。只需在检查
userSc的其他语句之前添加另一个
if
语句ore==“999”
while True:

    #This part gets the user input.  It waits until the user enters a valid number input.
    while True:
        prelim = input('Enter the score or type "999" to quit: ')
        try:
            prelim = int(prelim)
        except:
            print("Please enter a valid input.")
        else:
            #if the input can be converted into a number, then this is our final input value
            userScore = float(prelim)
            break

    #The first thing we should check is if the user wants to exit.  This way it won't print out an answer then exit.
    if userScore == 999:
        break

    if userScore >= 90:
        print ("You earned an A")


    elif userScore >= 80:
        print ("You earned a B")


    elif userScore >= 70:
        print ("You earned a C")


    elif userScore >= 60:
        print ("You earned a D")


    elif userScore <= 59.9:
        print ("You earned an F")