我的代码在哪里出错?

时间:2016-11-05 18:54:01

标签: while-loop helper

attempt = 0
answer = "canberra":
while answer != "canberra"    
    input("what is the capital of Australia: ?") 
print(attempt)

我不知道它有什么问题。基本上,任务是编写代码以反复询问用户问题,直到他们得到正确的答案。但是,当我运行我的代码时,即使我正确回答问题,它也会反复询问问题。我可能会用“if”语句来做,但是任务需要使用“while”循环请,如果有人可以帮助!!(尽管不要过于复杂。):)

2 个答案:

答案 0 :(得分:1)

  1. 您不清楚使用哪种编程语言。我认为这是Python:)
  2. 您应该在while循环中增加尝试计数器,如下所示:attempt += 1
  3. 在while循环的beginnig中,您将变量answer与字符串'canberra'进行比较。此比较返回False,因为变量answer内部已经有'canberra'。所以"canberra" != "canberra" => False
  4. 您在answer = "canberra":行的末尾有一个冒号,但它应位于下一行while answert ...的末尾。
  5. 代码:

    attempt = 0
    answer = ""
    correct_answer = "canberra"
    
    while answer != correct_answer:
        answer = input("what is the capital of Australia? ")
        attempt += 1
    print(attempt)
    

答案 1 :(得分:0)

您正在伪造while语句的括号,并且在堪培拉旁边也有一个“:”。

看看是否有帮助。

attempt = 0
answer = "canberra";
while (answer != "canberra"):
input("what is the capital of Australia: ?")
attempt += 1
console.log(attempt)
相关问题