首页 > 解决方案 > Input statements just keep going in a loop

问题描述

Hi i am albert i am learning python, in this block of code i wrote i intend it to print the total, but the input statements just keep going in a loop

print("this program prints your invoice")
while True:
    ID = input("item identification: ")
    if ID == "done":
        break
    if len(ID) < 3:
        print("identification must be at least 3 characters long")
        exit(1)
        break
    try:
        Quantity = int(input("quantity sold: "))
    except ValueError:
        print("please enter an integer for quantity sold!!!")
        exit(2)
    if Quantity <= 0:
        break 
        print("please enter an integer for quantity sold!!!")
        exit(3)
    try:
        price = float(input("price of item"))
    except ValueError:
        print("please enter a float for price of item!!")
        exit(4)
    if price <= 0:
        print("please enter a positive value for the price!!!")
        exit(5)
        break
cost = 0
total = cost + (Quantity*price)
print(total)

标签: python

解决方案


我想你需要这个

cost = 0
total = cost + (Quantity*price)
print(total)

在while循环内。否则,完全跳过循环。


推荐阅读