Python 存储“for”循环的编号';s输出,并将其相加?
我想存储整数并返回总和,对随机值使用Python 存储“for”循环的编号';s输出,并将其相加?,python,Python,我想存储整数并返回总和,对随机值使用random.randint() 代码如下所示: import random val = int(input('How many numbers?: ')) for i in range(val): print(random.randint(1,99)) 我需要将整数打印到控制台,然后求和并返回最终的和 例如: How many numbers?: 4 84 50 35 35 Final number: 204 我也需要它来处理无限多的数字。使
random.randint()
代码如下所示:
import random
val = int(input('How many numbers?: '))
for i in range(val):
print(random.randint(1,99))
我需要将整数打印到控制台,然后求和并返回最终的和
例如:
How many numbers?: 4
84
50
35
35
Final number: 204
我也需要它来处理无限多的数字。使用列表来存储数字。 使用
sum()
将它们相加。
使用join()
将它们打印出来
import random
val = int(input('How many numbers?: '))
numbers = [random.randint(1,99) for i in range(val)]
print('\n'.join(str(i) for i in numbers))
print('Final number: {}'.format(sum(numbers)))
示例输出:
How many numbers?: 5
60
70
51
65
18
Final number: 264
您将无法为无限系列的随机数提供总和,也无法存储它们。使用列表存储这些数字。 使用
sum()
将它们相加。
使用join()
将它们打印出来
import random
val = int(input('How many numbers?: '))
numbers = [random.randint(1,99) for i in range(val)]
print('\n'.join(str(i) for i in numbers))
print('Final number: {}'.format(sum(numbers)))
示例输出:
How many numbers?: 5
60
70
51
65
18
Final number: 264
您将无法为无限系列的随机数提供和,也无法存储它们。此解决方案仅涉及无终点的处理,对于问题的其他方面已经有了很好的解决方案。 没有办法对无限数量的数字求和,但您可以一直生成数字,直到程序接收到
键盘中断
nums = []
try:
while True:
nums.append(random.randint(1,99))
except KeyboardInterrupt:
pass
total = sum(nums)
amount_finished = len(nums)
或支持潜在但非必要的限制,您可以使用:
str_limit = input("enter the limit (accepts 'inf') ")
if str_limit=="inf":
limit = float("inf")
print ("you will need to stop the program with a KeyboardInterrupt")
else:
limit = int(str_limit)
nums = []
try:
while limit>0:
nums.append(random.randint(1,99))
limit-=1
except KeyboardInterrupt:
pass
total = sum(nums)
amount_finished = len(nums)
此解决方案仅涉及无终点的处理,对于问题的其他方面已经有了很好的解决方案。 没有办法对无限数量的数字求和,但您可以一直生成数字,直到程序接收到
键盘中断
nums = []
try:
while True:
nums.append(random.randint(1,99))
except KeyboardInterrupt:
pass
total = sum(nums)
amount_finished = len(nums)
或支持潜在但非必要的限制,您可以使用:
str_limit = input("enter the limit (accepts 'inf') ")
if str_limit=="inf":
limit = float("inf")
print ("you will need to stop the program with a KeyboardInterrupt")
else:
limit = int(str_limit)
nums = []
try:
while limit>0:
nums.append(random.randint(1,99))
limit-=1
except KeyboardInterrupt:
pass
total = sum(nums)
amount_finished = len(nums)
现在,您没有跟踪在val中打印的数字 为了跟踪和汇总所有数字,需要将它们存储在一个数组中
import random
val = int(input('How many numbers?: '))
#random number is stored as val
for i in range(val):
#this will run from 1-val
print(random.randint(1,99))
#these numbers are simply being printed, not stored
所以我们能做的就是存储数字,然后打印出来
sum=0
for i in range(val):
num=(random.randint(1,99))
sum=sum+num
print"random number:", num
print"The total sum is:", sum
现在,您没有跟踪在val中打印的数字 为了跟踪和汇总所有数字,需要将它们存储在一个数组中
import random
val = int(input('How many numbers?: '))
#random number is stored as val
for i in range(val):
#this will run from 1-val
print(random.randint(1,99))
#these numbers are simply being printed, not stored
所以我们能做的就是存储数字,然后打印出来
sum=0
for i in range(val):
num=(random.randint(1,99))
sum=sum+num
print"random number:", num
print"The total sum is:", sum
你可能需要解释他说的“编程新手”,“试验python”,他的用户名是Noobieatcode你可能需要解释他说的“编程新手”,“试验python”,他的用户名是Noobieatcode