python中的计算器类型程序出错

时间:2016-08-30 17:35:04

标签: python

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的新手,所以请在答案中原谅。

编辑3 -

在使用下面的答案后,我已经修复了脚本,最终得到了这个:

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'

在'你想要别的什么'之后我输入'是'。

2 个答案:

答案 0 :(得分:1)

首先,在您进行检查之前,打破。您的支票本身也会失败:

if answer==mean:
    mean

您已将答案(字符串)与均值(函数对象)进行了比较。尝试:

if answer == "mean":
    mean()

此外,我希望您将输入数字从字符串转换为 int

if number=='':
    ....
else:
    numbers=numbers+[int(number)]

答案 1 :(得分:1)

可能会对你有帮助。

numbers=[]

def means():
    end_mean = reduce(lambda x, y: x + y, numbers) / len(numbers)
    print(end_mean)

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()
        else:
            break

我从您的代码中更改了什么?

  • 添加了try-except块。添加此选项以便在您将''作为输入传递时可以处理该情况。
  • 在传递给号码之前将输入投放到int,因为您需要int值来计算均值,而不是string值。
  • 您正在从输入中创建一个列表,然后添加到上一个列表,这是不必要且低效的。我将其替换为numbers.append(number)
  • 删除了不必要的break语句。 break用于从循环中退出。当您使用break时,您将永远不会看到后面的语句被执行。