简体   繁体   English

Python,循环 - 代码结果与预期结果不匹配

[英]Python, loops - code results do not match expected outcome

I am trying to make a program for basic addition(+) on mathematics.我正在尝试为数学的基本加法(+)制作一个程序。 My code is as follows:我的代码如下:

   import random

   Totalrounds = input("Input how many rounds you want try? ")

   answerlist = list()


   for i in range(0, int(Totalrounds)):
        numb1 = random.randint(0,9)
        numb2 = random.randint(0,9)            
        print(numb1, "+" , numb2,)
        Answer = int(input("Your answer is? " ))

        if Answer == numb1 + numb2 :
              print("Your answer is correct")
              print()
        else:
              print("Your answer is wrong")
              print("the right answer is ", numb1 + numb2)
              print()  

        answerlist.append(Answer)

   #Result

   print("this is your Answersheet from" ,Totalrounds, "question")

   for i in range(0, len(answerlist)):
      if answerlist[i] == numb1 + numb2 :
         Result1 = "correct"
         print (numb1, "+" , numb2, "=" ,answerlist[i], Result1 ,)
      else:
         Result1 = "wrong"
         print (numb1, "+" , numb2, "=" ,answerlist[i], Result1 ,)

If I put totalrounds around 5, on #result print, the question 1 - 4 will have different number from upper list.如果我将总轮数放在 5 左右,在#result 打印上,问题 1 - 4 的数字将与上面的列表不同。 can anyone tell me what I did wrong in this code and what can i do to make it same like upper list question?谁能告诉我我在这段代码中做错了什么,我该怎么做才能使它像上面的问题一样? question = numb1 "+" numb2问题 = numb1 "+" numb2

thank you for you help谢谢你的帮助

There are several things that are problematic or style related in your code.在您的代码中有几件事是有问题的或与样式相关的。

In the second for loop you try to access the numb1 / numb2 but they are not relevant because each iteration of the first for loop you change them so to overcome this issue you need to store them as well.在第二个for循环中,您尝试访问numb1 / numb2但它们不相关,因为第一个for循环的每次迭代都会更改它们,因此为了克服这个问题,您还需要存储它们。

There is no need to set range to start from 0, this is the default value.无需设置range从 0 开始,这是默认值。

When initiating a list there is no need to do list() just [] is enough.启动list时,无需执行list()只需[]就足够了。

a correction to your code is as follows:对您的代码的更正如下:

import random

Totalrounds = input("Input how many rounds you want try? ")

answerlist = []
numb1_list = []
numb2_list = []


for i in range(int(Totalrounds)):
    numb1 = random.randint(0,9)
    numb1_list.append(numb1)
    numb2 = random.randint(0,9)
    numb2_list.append(numb2)
    print(numb1, "+" , numb2,)
    Answer = int(input("Your answer is? " ))
    if Answer == numb1 + numb2 :
          print("Your answer is correct")
          print()
    else:
          print("Your answer is wrong")
          print("the right answer is ", numb1 + numb2)
          print()

    answerlist.append(Answer)

#Result

print("this is your Answersheet from" ,Totalrounds, "question")

for i in range(len(answerlist)):
  if answerlist[i] == numb1_list[i] + numb2_list[i] :
     Result1 = "correct"
     print (numb1_list[i], "+" , numb2_list[i], "=" ,answerlist[i], Result1 ,)
  else:
     Result1 = "wrong"
     print (numb1_list[i], "+" , numb2_list[i], "=" ,answerlist[i], Result1 ,)

You store the values of the answers in answerlist but you only store the most recent values of numb1 and numb2 so they are always printed as the same.您将答案的值存储在answerlist中,但您只存储numb1numb2的最新值,因此它们始终打印为相同的值。

If you want to print all the previous questions you should store them in a list too.如果您想打印所有以前的问题,您也应该将它们存储在一个列表中。

Then change the print statement at the end然后改变最后的打印语句

print (numb1list[i], "+", numb2list[i], "=",answerlist[i], Result1)

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

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