简体   繁体   English

如果输入负数,则停止 while 循环

[英]Stop the while loop if enter the negative number

I've started with learning Python and now I am stuck with the WHILE loop, so if you could give me some advice, I would appreciate it.我是从学习 Python 开始的,现在我坚持使用 WHILE 循环,所以如果你能给我一些建议,我将不胜感激。

So, this is my code and what I need is to stop the whole code if I enter the negative number and to print ("Wrong entry"), but my code, on entering the negative number, still passes and prints me also ("Avg price:").所以,这是我的代码,如果我输入负数并打印(“错误输入”),我需要停止整个代码,但是我的代码在输入负数时仍然通过并打印我(“平均价格:")。 I would like not to print ("Avg price is: ") when I enter eg (2,3,6, -12) - only to print ("Wrong entry").当我输入例如 (2,3,6, -12) 时,我不想打印(“平均价格是:”) - 只打印(“错误输入”)。 I now that my last print is within WHILE loop, but I am trying to find the solutions :) Probably, there is easier solution for this, but as I said I am the newbie and still learning Thank you in advance.我现在最后一次打印在 WHILE 循环中,但我正在尝试找到解决方案 :) 可能有更简单的解决方案,但正如我所说,我是新手并且仍在学习 提前谢谢你。

price= int(input("Enter the price: "))

price_list=[]

while price!= 0:
    price_list.append(price)
    if price< 0:
       print("Wrong entry")
       break
    price=int(input())

    price_sum= sum(price_list)
print(f"Avg price is: {price_sum / len(price_list)}")

Using a while/else loop produces your desired behaviour.使用 while/else 循环会产生您想要的行为。

  • The code in the else doesn't run if the break in the while loop is encountered如果遇到 while 循环中的中断,则 else 中的代码不会运行

Code代码

price= int(input("Enter the price: "))

price_list=[]

while price!= 0:
    price_list.append(price)
    if price< 0:
       print("Wrong entry")
       break
    price=int(input())

    price_sum= sum(price_list)
else:
    print(f"Avg price is: {price_sum / len(price_list)}")

It's because it is outside the loop, it will always run.这是因为它在循环之外,它会一直运行。

So try this:所以试试这个:

price= int(input("Enter the price: "))

price_list=[]

while price != 0:
    if price< 0:
       print("Wrong entry")
       break
    price_list.append(price)
    price=int(input())            
    price_sum= sum(price_list)
if price > 0:
    print(f"Avg price is: {price_sum / len(price_list)}")

If you don't want to run rest of code when getting negative number, you can do something like this:如果您不想在获得负数时运行其余代码,您可以执行以下操作:

price= int(input("Enter the price: "))
ok = True
price_list=[]

while price!= 0:
    price_list.append(price)
    if price< 0:
       print("Wrong entry")
       ok = False
       break
    price=int(input())
if ok:
    price_sum= sum(price_list)
    print(f"Avg price is: {price_sum / len(price_list)}")

You have two options here.您在这里有两个选择。 You can include the check in the while guard, like this while price > 0 or you can use the keyword break and add an if guard inside the loop, like this你可以在 while 守卫中包含检查,像这样while price > 0或者你可以使用关键字break并在循环内添加一个 if 守卫,像这样

if price < 0:
    break

Since the first time you input the price it is outside the loop, the best course would be to add the guard to the while loop, so that it gets skipped in case first input is negative.由于您第一次输入价格时它在循环外,最好的方法是将保护添加到 while 循环中,以便在第一个输入为负的情况下跳过它。

You are doing procedural programming here.你在这里做程序化编程。 You print statement it outside the loop.你在循环外print语句。 Also, there is no control statement checking the statement.此外,没有检查语句的控制语句。 So regardless of the input, print statement is executed.所以不管输入如何,打印语句都会被执行。

Since any non empty list is a truth value, you can check if the list is empty or it contains some elements由于任何非空列表都是真值,您可以检查列表是否为空或包含某些元素

price= int(input("Enter the price: "))

price_list=[]

while price!= 0:
    
    if price< 0:
       print("Wrong entry")
       break
    price_list.append(price)
    price=int(input())
if price_list:
    price_sum= sum(price_list)
    print(f"Avg price is: {price_sum / len(price_list)}")

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

相关问题 Python 3+:While循环继续要求用户输入费用,即使用户输入了停止循环的按钮。 - Python 3+: While loop continues to ask user to Enter an Expense even if user inputs button to stop loop. 当数字是 2 的幂时,while 循环不会停止 - While loop doesn't stop when number is power of 2 我想做一个嵌套循环,让用户输入一个数字,该数字不会使输入变量具有负数 output。如何更改变量? - I want to make a nested loop that makes user enter a number that does not make the input variable have a negative output. How with changing variable? 停止数字为负的简单方法 - Simple way to stop a number going negative 开始/停止while循环? - Start/stop a while loop? 如何while循环停止 - How to while loop stop 使用 while 循环查找列表的总和,直到出现负数或列表结尾 - Use a while loop to find sum of list until negative number or end of list appears 在函数中使用 while 循环查找给定列表中第一次出现的负数 - finding the first occurrence of a negative number in a given list using while loop in a function 如果我输入大于 0.1 的数字,Numpy 数组会卡在 while 循环中 - Numpy array stuck in while loop if I enter a number greater than 0.1 我将如何使用while循环,以便如果他们输入数字,它将再次询问他们? - How would I use a while loop so that if they enter a number it would ask them the again?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM