我似乎无法让这个程序运行。没有错误,当我运行它时,什么都没有显示出来。任何帮助表示赞赏1
#Return multiple values
def load():
name=input("enter a name")
num1=int(input("Enter num1: "))
num2=int(input("Enter num2: "))
num3=int(input("Enter num3: "))
return name, num1, num2, num3
def calc(num1, num2, num3):
sum=num1, num2, num3
avg=sum/3
return sum, avg
def output(name, num1, num2, num3, avg, sum):
print("Your name is: ", name)
print("The 3 numbers are: ", num1, num2, num3)
print('The sum is: ',sum )
print("The average is: ", avg)
def main():
name, num1, num2, num3=load()
sum, avg=calc(num1, num2, num3)
output(name, num1, num2, num3, sum, avg)
答案 0 :(得分:1)
你需要调用main()
函数,这一行可能会抛出异常:
sum=num1, num2, num3
avg=sum/3
将其更改为:
sum=num1 + num2 + num3
avg=sum/3
答案 1 :(得分:1)
作为前面提到的友好评论者,你没有调用你的main()例程(参见最后一行)。您还在sum
的签名中混合了avg
和output
次调用的序列。此外,sum
是Python内置函数。避免那些。我使用了raw_input
,因为我使用的是Python 2.x,请不要让您感到困惑。我对Python 2.x的使用也是你在我的输出中看到额外括号的原因。
总的来说,你非常接近一个有效的解决方案,对初学者来说很好!
#Return multiple values
def load():
name=raw_input("enter a name: ")
num1=int(raw_input("Enter num1: "))
num2=int(raw_input("Enter num2: "))
num3=int(raw_input("Enter num3: "))
return name, num1, num2, num3
def calc(num1, num2, num3):
sum1=num1+num2+num3 # sum is a python keyword
avg=sum1/3.0 # avoiding integer division in case you are using python 2.x
return sum1, avg
def output(name, num1, num2, num3, sum1, avg):
print("Your name is: ", name)
print("The 3 numbers are: ", num1, num2, num3)
print('The sum is: ',sum1 )
print("The average is: ", avg)
def main():
name, num1, num2, num3=load()
sum1, avg=calc(num1, num2, num3)
output(name, num1, num2, num3, sum1,avg)
main()
输出:
enter a name: Anton
Enter num1: 1
Enter num2: 2
Enter num3: 3
('Your name is: ', 'Anton')
('The 3 numbers are: ', 1, 2, 3)
('The sum is: ', 6)
('The average is: ', 2.0)