代码中的一些问题
实际上你可以简化你的代码
-
您可以将所有数字作为单个字符串的输入
-
Split
通过使用
string.split
-
将每个字符串转换为整数并计算和,同时将整数追加到列表中
-
计算平均值并打印
-
使用切片反转列表
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