Skip to main content
 首页 » 编程设计

python之无法让 FOR 循环工作

2025年05月04日15bhlsheji

下面,我插入了一些代码,除了大多数 Python 新手用户之外,其他人都明显错误。

请问大家能否对代码提出一些新手改进建议?如逻辑变量名、添加注释等。

根据问题中的要求,我们也将感谢对代码中错误的更正。


I'm having a problem with getting the loop below to work. Does anybody have a suggestion as to what's wrong? At the moment I get asked the question, and then the code shows an error.

What should happen is that I am asked a question, and then have 3 chances to answer correctly.

a = input("What is the opposite to night?") 
 
for xx in range(0,3) 
    if a == Night: 
    print("That's right! Well done") 
    else: 
    print("Sorry, try again") 

请您参考如下方法:

请看下面更正:

a = input("What is the opposite to night?") 
 
for x in range(0,3): # must have colons at the end of for statement 
    if a.lower() == "night":   # we should accept all cases 
      print("That's right! Well done") # indentation required in if statement 
    else: 
      print("Sorry, try again") # indentation required also