简体   繁体   English

打破循环Python时不要停止简单

[英]Break not Stopping Simple While Loop Python

Python noob here. Python noob在这里。 I am trying to create a list with numbers a user inputs and then do some simple calculations with the numbers in the list at the end, in a while loop. 我正在尝试使用用户输入的数字创建一个列表,然后使用while循环中列表中的数字进行一些简单的计算。 The While loop is not breaking when 'done' is inputted. 输入“完成”时,While循环不会中断。 It just prints 'Invalid input.' 它只打印'无效输入'。

list = []
while True:
    try:
        n = int(input('Enter a number: '))
        list.append(n)
    except:
        print('Invalid input') 
    if n == 'done':
        break

print(sum.list())
print(len.list())
print(mean.list())

This is because the int() function is trying to convert your input into an integer, but it is raising an error because the string 'done' can not be converted to an integer. 这是因为int()函数试图将您的输入转换为整数,但它会引发错误,因为字符串'done'无法转换为整数。 Another point is that sum(), mean() and len() are functions, not attributes of lists . 另一点是sum(),mean()和len()是函数,而不是列表的属性。 Also mean() is not a built in function in python, it must be import with numpy . mean()也不是python中的内置函数,必须用numpy导入。 Try it like this: 试试这样:

from numpy import mean
list = []
while True:
    try:
        n = input('Enter a number: ')
        list.append(int(n))
    except:
        if n!='done':
            print('Invalid input') 
    if n == 'done':
        break

print(sum(list))
print(len(list))
print(mean(list))

You will have to separate receiving user input with checking against "done" from conversion to a number and appending to the list. 您必须将收到的用户输入与转换为数字的“完成”进行检查并附加到列表中。 And you will have to check for "done" before converting the input to an integer. 在将输入转换为整数之前,您必须检查“完成”。

Try something like this: 尝试这样的事情:

list_of_numbers = []

while True:
    user_input = input("Enter a number or 'done' to end: ")

    if user_input == "done":
        break

    try:
        number = int(user_input)

    except ValueError:
        print("invalid number")
        continue

    list_of_numbers.append(number)

print(list_of_numbers)

# further processing of the list here

You must check if you can turn the input into a integer before appending to your list. 在附加到列表之前,您必须检查是否可以将输入转换为整数。 You can use use the try/except to catch if the input variable is convertible to a integer. 您可以使用try / except来捕获输入变量是否可以转换为整数。 If it's not then you can check for done and exit. 如果不是那么你可以检查done并退出。

list = []
while True:
    n = input('Enter a number: ')
    try:
        n = int(n)
        list.append(n)
    except ValueError:
        if n == 'done':
            break
        print('Invalid input') 

total = sum(list)
length = len(list)
mean = total/length

print('sum:', total)
print('length:', length)
print('mean:', mean)

Example interaction 示例交互

Enter a number: 12
Enter a number: 3
Enter a number: 4
Enter a number:
Invalid input
Enter a number: 5
Enter a number:
Invalid input
Enter a number: done
sum: 24
length: 4
mean: 6.0

If the user enters done , you will attempt to convert into an int , which will raise an exception that you then catch. 如果用户输入done ,您将尝试转换为int ,这将引发您随后捕获的异常。

Instead, perform your check before attempting to convert it to an integer. 相反, 尝试将其转换为整数之前执行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM