[英]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 循环会产生您想要的行为。
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.