简体   繁体   English

如何在 for 循环中添加 readline 的 output

[英]How to add the output of a readline inside a for loop

I am trying to code a loop that will find a test average for a certan number of tests that the user defines.我正在尝试编写一个循环,该循环将为用户定义的某些测试找到测试平均值。 I need to add the output/input (the number you type in the console) of all the readlines that run in the loop.我需要添加循环中运行的所有 readlines 的输出/输入(您在控制台中键入的数字)。

i have made a variation of this project in the past with a set number of test scores you can input but i have no experience adding the readlines in a loop我过去对这个项目做了一个变体,你可以输入一定数量的测试分数,但我没有在循环中添加 readlines 的经验

test_no = int(input("How Many Tests Did You Take: "))
for x in range (1, test_no+1):
    t_score = int(input("The Score on Test %s Was: " % x)) 
average = t_score/test_no
print (average)

i am expecting the output to have added all the outputs of the readline function inside the loop and ten divided by the number of times the loop runs我期待 output 在循环内添加了 readline function 的所有输出,并且十除以循环运行的次数

Like @ForceBrue commented, you are not adding scores, instead overwriting.就像@ForceBrue 评论的那样,您没有添加分数,而是覆盖。 I guess this is what you want to achieve:我想这就是你想要实现的目标:

test_no = int(input("How Many Tests Did You Take: "))
t_score = 0
for x in range (1, test_no+1):
    t_score += int(input("The Score on Test %s Was: " % x)) 
average = t_score/test_no
print (average)

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

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