如何汇总数字列表?

时间:2019-03-11 03:03:22

标签: python

我的程序需要从用户读取一组整数并将其存储在列表中。它应该从用户读取数字,直到用户输入0退出为止。然后,需要将它们加起来并显示给用户。如果用户键入的不是整数,则需要向用户显示错误。

我很难弄清楚如何列出用户可以无限输入数字的列表。

根据我尝试做的工作和查找的内容,我只能制作一个已定义的列表,并制作一个程序,并接受一定数量的输入。

这就是我所拥有的。显然,它不起作用,但是它显示了我要做什么。

n, ns = 0, 0  # count of numbers, sum of numbers
print("Enter numbers or any other 0 to quit.")

while(1):
     grades = input("enter = ")
     if (int):
          ns += int
          n += 1
     else:
          if (0):
               print(ns)
          break

2 个答案:

答案 0 :(得分:1)

使用此:

In [11]: np.nextafter(np.longdouble(0),1)
Out[11]: 4e-4951

答案 1 :(得分:0)

  

Python 3

#!/bin/bash
read -p "How many directories would you like?" num_folder
if test $num_folder -lt 10
then
    for ((i=0; i<num_folder; i++)); do
      mkdir folder$i        
    done

    tree -c

    read -rsp "Press enter to continue"
    clear

else
    echo "Please write a number between 1 and 10"      
fi
相关问题