python

时间:2016-08-30 20:00:02

标签: python

这是我的脚本,它的脚本就像计算器一样,但是当我运行它时,它为while循环提供了无效的语法?我是python的新手,请帮助我。

import functools
numbers=[]

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

def sums():
    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":
            mean()
        elif answer == 'sum':
            sums()
        elif answer== 'median':
            median()

def median():
    numbers.sort()
    medianNumber=int(len(numbers))
    if medianNumber%2==0:
        end_median=numbers[int(len(numbers))/2]+numbers[int(len(numbers))/2+1]
    if medianNumber%2==1:
        numbers[int(len(numbers))+1/2

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":
            mean()
        elif answer == 'sum':
            sums()
        elif answer== 'median':
            median()
        print('Do you want anything else?')
        reply=input()
        if reply=='no':
            break
        elif reply=='yes':
             whatDo()
        else:
            break

我确实删除了while循环,但之后它说打印功能无效。请记住,我是python的新手。

2 个答案:

答案 0 :(得分:2)

在while循环之前,将结束括号添加到该行:

numbers[int(len(numbers))+1/2]

通常最好总是检查上面发生错误的行,如果python告诉你发现了SyntaxError,但你的语法似乎有效。

答案 1 :(得分:0)

while循环之前,缺少使用]列表的numbers

那一行应该是:

numbers[int(len(numbers))+1/2]