我想编写一个程序,询问用户要处理多少个数字,然后询问那么多数字。然后它将找到已处理的总数和最大值。输出示例:
Enter the number of values to process: 5
First value: 3
Next value: 9.2
Next value: -2.5
Next value: 0.25
Next value: 6
The total is 15.95
The maximum is 9.20
有一件事我迷失的是,如果,如果我有代码for num in (1, 3)
和num
被分配多个值,我如何提取这两个值?
设置example_list = list()
,然后for
循环执行,每次您要求输入时,通过将值添加到列表中example_list.append(input_response)
。
使用相同的for
循环提取出值,以循环遍历列表。
example_list = list()
total = 0
max_value = 0
for stored_number in example_list:
# Ask for input and store in input_response variable
example_list.append(input_response)
# Here is where you could add the values to a counter variable,
# as well as check whether they are the maximum
total += stored_number
if stored_number > max:
max_value = stored_number
# Display results
对于您想要的东西,您也可以只使用内置函数max和sum:
max_value = max(example_list)
total = sum(example_list)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句