Py学习  »  Python

如何将iterable赋给python数组变量?

jedimilk • 5 年前 • 1422 次点击  

我正在定义一个包含20个元素的数组,然后要求用户为每个元素输入一个数字,然后它将平均它们输入的所有数字。然而,我完成这项工作的方式非常麻烦,而且看起来像是可以简化的重复代码。但是我很难找到答案。我认为可以在每个输入之后增加下标,以避免要求20行输入。

我试过加入一个开始/停止/步骤解决方案。但是我所做的是错的。我尝试的是num[0:19:1]=int(input(“type in number:”))希望这能完成3件事。

  1. 从0开始
  2. 在20号结束

然而,这并不奏效。

START = input("Type start if you wan't to do this thang: ")
while START == "start":
    num = ([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,])
    num[0] = int(input("type in number: "))
    num[1] = int(input("type in number: "))
    num[2] = int(input("type in number: "))
    num[3] = int(input("type in number: "))
    num[4] = int(input("type in number: "))
    num[5] = int(input("type in number: "))
    num[6] = int(input("type in number: "))
    num[7] = int(input("type in number: "))
    num[8] = int(input("type in number: "))
    num[9] = int(input("type in number: "))
    num[10] = int(input("type in number: "))
    num[11] = int(input("type in number: "))
    num[12] = int(input("type in number: "))
    num[13] = int(input("type in number: "))
    num[14] = int(input("type in number: "))
    num[15] = int(input("type in number: "))
    num[16] = int(input("type in number: "))
    num[17] = int(input("type in number: "))
    num[18] = int(input("type in number: "))
    num[19] = int(input("type in number: "))
    average = (num[0] + num[1] + num[2] + num[3] + num[4] + num[5] + num[6] + num[7] + num[8] + num[9] + num[10] + num[11] + num[12] + num[13] + num[14] + num[15] + num[16] + num[17] + num[18]+ num[19]) / 20
    START = input("If that was wild for you, type start again, or type quit if that was lame: ")
    if START == "quit":
        break
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/57051
 
1422 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Hasee Amarathunga
Reply   •   1 楼
Hasee Amarathunga    6 年前

试试这个

def iterator(n):
    START = input("Type start if you wan't to do this thang: ")
    numbers=[]
    while START == "start":
        total=0
        for i in range(0,n):
            num=int(input("type in number: "))
            total+=num
            numbers.append(num)
        print("Average : ",total/n)
        print(numbers)

        START = input("If that was wild for you, type start again, or type quit if that was lame: ")
        if START == "quit":
            break

iterator(20)
Devesh Kumar Singh
Reply   •   2 楼
Devesh Kumar Singh    6 年前

代码中的一些问题

  • 实际上,你可以在输入时对数字求和,而不是在最后

实际上你可以简化你的代码

  1. 您可以将所有数字作为单个字符串的输入
  2. Split 通过使用 string.split
  3. 将每个字符串转换为整数并计算和,同时将整数追加到列表中
  4. 计算平均值并打印
  5. 使用切片反转列表 nums[::-1] 然后打印出来
start = input("Type start if you wan't to do this thang: ")

#If start is inputted, check it and start the loop,
if start.lower() == 'start':
    #Start an infinite loop
    while True:
        #Take 20 numbers separated by single space
        s = input("Enter 20 numbers separated by a single space")

        #List of numbers
        nums = []
        sum = 0

        #Split the string by whitespace, iterate over it and take the sum
        for item in s.split():
            num = int(item)
            nums.append(num)
            sum += int(num)

        #Calcuate the average
        avg = sum/20
        print('Average: ',avg)

        #Reverse the numbers by using reversed function
        nums = list(reversed(nums))

        #Print numbers
        print('The numbers in reverse are ', nums)
        #Check if quit was inputted, if yes break the loop, else continue
        START = input("If that was wild for you, type start again, or type quit if that was lame: ")
        if START == "quit":
            break

输出将如下所示

Type start if you wan't to do this thang: start
Enter 20 numbers separated by a single space1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
Average:  3.0
The numbers in reverse are  [5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1, 5, 4, 3, 2, 1]
If that was wild for you, type start again, or type quit if that was lame: start
Enter 20 numbers separated by a single space1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
Average:  5.5
The numbers in reverse are  [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
If that was wild for you, type start again, or type quit if that was lame: quit