当第一次输入数据时,它不会运行calc函数,只是从main()重置。一旦第二次输入数据,数据就被输入到calc函数中。我真的很感激你的帮助!
def main():
numbers = get_val()
get_calc(numbers)
def get_val():
print("Please enter 10 numbers:")
values = []
for i in range(10):
value = int(input("Enter a number:"))
values.append(value)
return values
def get_calc(numbers):
list1 = get_val()
print(list1)
print("The lowest number is", min(numbers))
print("The highest number is", max(numbers))
print("The sum of the numbers is", sum(numbers))
print("The average of the numbers is", sum(numbers)/len(numbers))
main()
答案 0 :(得分:0)
您已拨打get_val()
两次。第一次,您在main()
中调用它:
def main():
numbers = get_val()
您第二次在get_calc()
中调用它:
def get_calc(numbers):
list1 = get_val()
print(list1)
...
这意味着,一旦您调用main,它就会计算numbers
并将其传递给get_calc()
。但是,在get_calc()
中,您还会计算另一个数字列表:list1
。您打印它,然后继续使用numbers
。
应该是:
def main():
numbers = get_val()
get_calc(numbers)
def get_val():
print("Please enter 10 numbers:")
values = []
for i in range(10):
value = int(input("Enter a number:"))
values.append(value)
return values
def get_calc(numbers):
print(numbers)
print("The lowest number is", min(numbers))
print("The highest number is", max(numbers))
print("The sum of the numbers is", sum(numbers))
print("The average of the numbers is", sum(numbers)/len(numbers))
main()
答案 1 :(得分:0)
def get_calc(numbers):
list1 = numbers #I think this is what you want.
print(list1)
print("The lowest number is", min(numbers))
print("The highest number is", max(numbers))
print("The sum of the numbers is", sum(numbers))
print("The average of the numbers is", sum(numbers)/len(numbers))