Python 循环不中断
因此,我有一个python程序,它有多个可能的输入,对于这一部分,我试图打破while循环,该循环要求“输入产品成本”。然而,当用户输入有效成本时,它只会重新提示他们输入另一个成本,而不是结束Python 循环不中断,python,Python,因此,我有一个python程序,它有多个可能的输入,对于这一部分,我试图打破while循环,该循环要求“输入产品成本”。然而,当用户输入有效成本时,它只会重新提示他们输入另一个成本,而不是结束 while True: update_choice = input("What would you like to update? (c)ost or (q)uantity") if update_choice == "c": while True: new_pri
while True:
update_choice = input("What would you like to update? (c)ost or (q)uantity")
if update_choice == "c":
while True:
new_price_update = float(input("Enter a product cost: "))
if new_price_update > 0:
for i in range(0, len(product_names)):
if update_item == product_names[i]:
product_costs[i] = new_price_update
print("Product cost has been updated.")
break
else:
print("Invalid price. Please try again.")
break
仅退出当前循环。由于您打断了最内部的for循环,因此这只会退出此循环。您将进入第二个while循环
while True:
new_price_update = float(input("Enter a product cost: "))
if new_price_update > 0:
for i in range(0, len(product_names)):
if update_item == product_names[i]:
product_costs[i] = new_price_update
print("Product cost has been updated.")
break
您正在使用双循环语句:
为True时:
和用于范围内的i…
break语句将只退出它所在的最里面的循环,即for
循环,但将继续在循环中循环,而True:
循环将再次提示用户
请尝试以下方法:
while True:
new_price_update = float(input("Enter a product cost: "))
if new_price_update > 0:
for i in range(0, len(product_names)):
if update_item == product_names[i]:
product_costs[i] = new_price_update
print("Product cost has been updated.")
break
break
插入另一个
break
停止内部,而True:
,因为您的break
语句只会停止的,如下所示:
while True:
update_choice = input("What would you like to update? (c)ost or (q)uantity")
if update_choice == "c":
while True:
new_price_update = float(input("Enter a product cost: "))
if new_price_update > 0:
for i in range(0, len(product_names)):
if update_item == product_names[i]:
product_costs[i] = new_price_update
print("Product cost has been updated.")
break
else:
print("Invalid price. Please try again.")
break
您的中断
只会“中断”内部for循环。要中断第二个while循环,需要将中断
置于for循环之外
while True:
update_choice = input("What would you like to update? (c)ost or (q)uantity")
if update_choice == "c":
while True:
new_price_update = float(input("Enter a product cost: "))
if new_price_update > 0:
for i in range(0, len(product_names)):
if update_item == product_names[i]:
product_costs[i] = new_price_update
print("Product cost has been updated.")
break
else:
print("Invalid price. Please try again.")
如果在用户成功更新cost
后,您希望中断整个过程,请尝试以下代码:
# while loop (1)
while True:
# flag to indicate updated successfully
updated = False
update_choice = input("What would you like to update? (c)ost or (q)uantity")
if update_choice == "c":
# while loop (2)
while True:
new_price_update = float(input("Enter a product cost: "))
if new_price_update > 0:
# for loop (1)
for i in range(0, len(product_names)):
if update_item == product_names[i]:
product_costs[i] = new_price_update
print("Product cost has been updated.")
updated = True
# break out from for loop (1)
break
else:
print("Invalid price. Please try again.")
if (updated == True):
# break out from while loop (2)
break
if (updated == True):
# break out from while loop (1)
break
什么是product\u names
?break
只中断当前循环,在您的情况下,这是for i…
。break
位于for
循环中,Python一次只中断一个嵌套级别。您可以设置一个变量,然后从for循环中断,如果该变量为true,则再次中断。