语法错误'break outside loop'

时间:2016-06-24 17:05:57

标签: python function

基本上,我正在尝试创建一个可以接受输入的主函数,并使用该输入列表通过使用另一个函数来计算中值,模式和平均值。我是一名新手编写者(参加我的第一堂课),所以任何能帮助我指明正确方向的帮助都会非常感激。

def median(alist):

    srtd = sorted(alist)
    mid = len(alist) // 2 

    if len(alist) % 2 == 0:
        return (srtd[mid -1] + srtd[mid]) / 2.0
    else: 
        return (srtd[mid]) 

def main(): 

    yourlist = []
    addons = list(float(input("Enter your list of number to be calculated(stop to stop): ")))


    if addons != "stop": 
        data.append(yourlist)

    elif addons == "stop":      
        break
    else:
        print("Bad input, try again: ")
        continue

    medresult = median(yourlist)
    return medresult


print(medresult)

2 个答案:

答案 0 :(得分:1)

continuefor仅用于whiledef main(): yourlist = [] while True: addons = input("Enter your list of number to be calculated(stop to stop): ") if addons != "stop": data.append(yourlist) elif addons == "stop": break else: # This block will never run because an input can either equal or not equal "stop" print("Bad input, try again: ") continue 等循环

所以你的main()应该有一个while循环。

SELECT sum(clicks.value) 
   FROM clicks 
   WHERE clicks.user IN 
       (SELECT users.username from users WHERE users.referrer = "janosuser")

答案 1 :(得分:0)

个人偏好,但我会将你的if语句更改为

if 'stop' not in addons:
    data.append(yourlist)

和elif到

elif not all(isinstance(number, float) for number in addons):
    print("Bad input, try again")

这将检查插件列表中的每个输入数字,看它是否为浮点数。如果不是所有都是浮点数,则再次打印尝试消息