将x的值相加并在#Python中打印总计

时间:2020-06-08 10:02:35

标签: python printing

此代码有效,我的问题是如何在末尾打印出总价值'x'

      import random
      bat1 = input('enter name: ')
      x = ()
      while x != 0:
        x = random.randint(0,6)
        print(bat1 + " " + str(x))

Python Newbee

3 个答案:

答案 0 :(得分:1)

检查此代码:

IVsHierarchy hierarchy;
                    solution.GetProjectOfUniqueName(projectFileName, out hierarchy);
                    Guid guidProjectID;
                    solution.GetGuidOfProject(hierarchy, out guidProjectID);
                    solution4.ReloadProject(guidProjectID);

您可以使用初始化为import random bat1 = input('enter name: ') x = () buffer = 0 while x != 0: x = random.randint(0, 6) buffer += x print(bat1 + " " + str(x)) print('total ' + str(buffer )) 的{​​{1}},在每个循环中您将buffer的值相加。输出示例:

0

答案 1 :(得分:1)

这是您要寻找的吗?

import random

bat1 = input('enter name: ')
x = ()
numbers = []
while x != 0:
    x = random.randint(0, 6)
    numbers.append(x)
    print(bat1 + " " + str(x))

print("Sum of all x: ", sum(numbers))

希望有帮助。

答案 2 :(得分:0)

您应该将所有xs相加!

import random
bat1 = input('enter name: ')
x = ()
sum = 0
while x != 0:
    x = random.randint(0,6)
    print(bat1 + " " + str(x))
    sum+=x
print("Sum:",sum)