numbers=[]
while True:
print('Input Number '+str(len(numbers)+1)+' (or nothing to close):')
number=input()
numbers=numbers+[number]
if number=='':
print('What do you want to do?')
answer=input()
break
if answer==mean:
mean
def mean():
end_mean=reduce(lambda x, y: x + y, numbers)/len(numbers)
print(end_mean)
我想在python中制作一个计算器类型程序,它允许您输入数字列表,然后选择您使用它们执行的操作。上面的脚本只是一个开始,但是当我输入数字并输入'mean'时,它会询问我要做什么,它会结束脚本并不显示任何内容。我是python的新手,所以请在答案中原谅。Python中的计算器类型程序错误
import functools
numbers=[]
def means():
end_mean = functools.reduce(lambda x, y: x + y, numbers)/len(numbers)
print(end_mean)
def sum():
end_sum = functools.reduce(lambda x, y: x + y, numbers)
print(end_sum)
def whatDo():
print('Input Extra Numbers '+str(len(numbers)+1)+' (or nothing to close):')
try:
number= int(input())
numbers.append(number)
except:
print('What do you want to do?')
answer = input()
if answer == "mean":
means()
while True:
print('Input Number '+str(len(numbers)+1)+' (or nothing to close):')
try:
number= int(input())
numbers.append(number)
except:
print('What do you want to do?')
answer = input()
if answer == "mean":
means()
print('Do you want anything else?')
reply=input()
if reply=='no':
break
elif reply--'yes':
whatDo()
else:
break
但是我得到这个:
Traceback (most recent call last):
File "E:/Python/calculator.py", line 26, in <module>
number= int(input())
ValueError: invalid literal for int() with base 10: ''
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:/Python/calculator.py", line 37, in <module>
elif reply--'yes':
TypeError: bad operand type for unary -: 'str'
后的“你 -
编辑3
使用回答下面我有固定的脚本与此有关结束后想要别的东西“,我输入'是'。
你的'break'在'if'检查前是否应答=='意思是':'(我加了引号)和'mean'应该是'mean()'来调用一个函数 – depperm
什么是python版本您正在使用? python2或python3? –
python 3 @AhsanulHaque –