简体   繁体   English

python while循环意外行为

[英]python while loop unexpected behavior

I'm relatively new to Python, and I don't understand the following code produces the subsequently unexpected output: 我对Python比较陌生,我不明白以下代码会产生后续的意外输出:

x = input("6 divided by 2 is")
while x != 3:
    print("Incorrect. Please try again.")
    x = input("6 divided by 2 is")
    print(x)

the output of which is: 其输出是:

6 divided by 2 is 3
Incorrect. Please try again.
6 divided by 2 is 3
3
Incorrect. Please try again.
6 divided by 2 is 

Why is the while loop still being executed even though x is equal to 3? 为什么即使x等于3,while循环仍在执行?

input() returns a string, which you are comparing to an integer. input()返回一个字符串,您将其与整数进行比较。 This will always return false. 这将始终返回false。 You'll have to wrap input() in a call to int() for a valid comparison. 你必须在对int()的调用中包装input()以进行有效的比较。

x = int(input("6 divided by 2 is"))
while x != 3:
    print("Incorrect. Please try again.")
    x = int(input("6 divided by 2 is"))
    print(x)

Read more on int() here . 这里阅读更多关于int()

You are getting this error is because you are not parsing the input like so: 您收到此错误是因为您没有像这样解析输入:

x = int(input("6 divided by 2 is"))

If you replace your inputer statement with that, it'll work. 如果用这个替换你的inputer语句,它就会起作用。

input method gives the string. input方法给出了字符串。 So you need to typecast to int as: 所以你需要将int转换为int:

x = int(input("6 divided by 2 is"))

Here is my answer to your question 这是我对你的问题的回答

Guesses = 0
while(Guesses < 101):
    try:
        x = int(input("6 divided by 2 is: "))
        if(x == 3):
            print("Correct! 6 divide by 2 is", x)
            break
        else:
            print("Incorrect. try again")
            Guesses += 1
    except ValueError:
        print("That is not a number. Try again.")
        Guesses += 1
else:
    print("Out of guesses.")

I am assuming you wanted the user to input a number so i put your code into a while\\else loop containing a try\\except loop . 我假设您希望用户input一个number所以我将您的代码放入包含try\\except loopwhile\\else loop中。 The try\\except loop ensures that if the users inputs a number, a ValueError will appear and it will inform them that what they inputted was not a number . try\\except循环确保如果用户inputs数字,将出现ValueError ,它将通知他们inputted was not a number The while\\else loop ensures that the user will be inputted the question if the Guesses limit is no higher than 100 . while\\else loop确保如果Guesses限制不高于100Guesses用户输入问题。 This code will ensure that if the user guesses the answer which is 3 , the user will be prompted that they got the answer right and the loop will end ; 此代码将确保如果用户guesses the answer3 ,将提示用户they got the answer right and the loop will end ; if the users guesses anything besides 3 (Number wise) the answer will be incorrect and the user will be prompted the question again ; 如果用户猜到除了3(数字)之外的任何内容, answer will be incorrect并且will be prompted the question again用户will be prompted the question again ; if the user guesses a string it will be classified as a ValueError and they will be notified that their answer wasn't a number and that the user has to try again . 如果user guesses a string ,它将被归类为ValueError,并且会通知their answer wasn't a number and that the user has to try again

Considering this was asked a long time ago, I'm assuming your probably forgot about this or you figured out the answer but if not try this and tell me if you like this answer. 考虑到这是很久以前的问题,我假设你可能已经忘记了这一点,或者你想出了答案,但如果没有尝试这个并告诉我你是否喜欢这个答案。 Thank :) 谢谢 :)

I actually tried this myself now with python 2.6, and did get an int without converting to int. 我现在自己尝试使用python 2.6,并且在没有转换为int的情况下获得了一个int。 For example, when I try the following: 例如,当我尝试以下操作时:

x = input("6 divided by 2 is")
print "Your input is %s, %s" % (x, type(x))

I get the following: 我得到以下内容:

6 divided by 2 is 2
Your input is 2, <type 'int'>

So is this a version issue? 这是一个版本问题吗? Or maybe an environment issue (I'm using OS X)? 或者可能是环境问题(我正在使用OS X)? What I do conclude is that it should be a good practice using previous recommendations using int(). 我的结论是,使用int()使用以前的建议应该是一个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM