简体   繁体   English

初学者:虽然循环无法正常运行,但语法错误,未显示任何内容

[英]Beginner: While loop not functioning properly, syntax errors, displays nothing

I am working through some Looping exercises, While statements in particular. 我正在通过一些Looping练习,尤其是While语句。 Here are the instructions: 以下是说明:

2.2) Modify the program so that it asks users whether they want to guess again each time. 2.2)修改程序,使其每次询问用户是否要再次猜测。 Use two variables, number for the number and answer for the answer to the question whether they want to continue guessing. 使用两个变量,数字表示数字,答案用于回答问题是否要继续猜测。 The program stops if the user guesses the correct number or answers "no". 如果用户猜测正确的数字或​​回答“否”,程序将停止。 (In other words, the program continues as long as a user has not answered "no" and has not guessed the correct number.) (换句话说,只要用户未回答“否”并且未猜到正确的数字,程序就会继续运行。)

Here is my code: 这是我的代码:

#!usr/bin/env python
#
#While statement

number = 24

while number != 24:
    answer = raw_input("Guess my lucky number! Do you want to keep guessing?")

    if number == 24:
       print "You got it! That is great!"

    elif answer == "no":
       print "Thank you for playing."

    else:
       print "That is not the right answer! Try again."

When I run the module in IDLE, the end quote of That is great!" - becomes red and says invalid syntax. In terminal if I run $ python while.py nothing loads. I've tried writing this as Python 3 functions with print("") but it still does not run. 当我在IDLE中运行模块时,That的结尾引号很棒!”-变为红色并表示语法无效。在终端中,如果我运行$ python while.py没有加载。我尝试将其编写为带有print的Python 3函数(“”),但它仍然无法运行。

Thanks to anyone who can help with this. 感谢任何可以帮助您的人。

The while-cycle is never entered because the condition number != 24 never holds. 永远不会输入while循环,因为条件number != 24永远不会成立。 This is why there is no output. 这就是为什么没有输出的原因。

Your loop never executes because you state that number = 24, and then right after, your while loop will only start if number != 24. 您的循环永远不会执行,因为您声明数字= 24,然后紧接着,仅当数字!= 24时您的while循环才会开始。

In addition, raw_input will yield a string, not an int so either ask for "24" or cast the raw_input to an int. 另外,raw_input将产生一个字符串,而不是一个int,因此要求输入“ 24”或将raw_input强制转换为int。

It also seems that you don't actually give the user a chance to guess the number at all; 看来,您实际上并没有给用户提供任何机会来猜测这个数字。 you only ask the user if s/he wants to keep playing. 您只问用户是否想继续玩。

You might want to do something like this: 您可能想要执行以下操作:

number = 24
answer = ""
while answer != str(number):
    answer = raw_input("Guess my lucky number, or type 'no' to quit.")
    if answer == "no":
        print "Okay, see you later"
        break
    elif answer != str(number):
        print "wrong number"

if answer == str(number):
    print "you got it right"

Here's the syntax issues: 这是语法问题:

answer = ""
while answer != "24":
    answer = raw_input("Guess my lucky number! Do you want to keep guessing?")
    if answer == "24":
        # You can fill in the rest ...

Well, I don't want to straight out solve it for you, but take a look at your conditional in the while loop. 好吧,我不想直接为您解决问题,而是在while循环中查看您的条件。 Think about what happens, line-by-line, when you run it, particularly in the "number" variable. 在运行时逐行考虑会发生什么,尤其是在“ number”变量中。

The other answers are touching on the problem but there are more... 其他答案正在解决这个问题,但还有更多...

Yes you really should be checking the answer variable in your loop instead of the number variable, but also keep in mind that raw_input is going to give you a string. 是的,您确实应该在循环中检查答案变量,而不是数字变量,但还要记住,raw_input将为您提供一个字符串。 So you will not get an int 24, you will get a string "24". 因此,您将不会获得整数24,而将获得字符串“ 24”。 The idea here is to take the answer variable from raw_input and check that variable against both your number and the value "no" 这里的想法是从raw_input中获取答案变量,并根据您的数字和值“ no”检查该变量

number = "24" 数字=“ 24”

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

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