0

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)
4

2 回答 2

1

我想你需要这个

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

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

于 2021-03-02T12:38:37.490 回答
0

尝试这个:

print("This program prints your invoice")
total = 0
more_to_add = True

while more_to_add == True:
  ID = input("Item identification: ")

  if len(ID) < 3:
    print("Identification must be at least 3 characters long")
    continue
  try:
    Quantity = int(input("Quantity sold: "))
  except ValueError:
    print("Please enter an integer for quantity sold!!!")
    continue
  if Quantity <= 0:
    print("Please enter an integer for quantity sold!!!")
    continue
  try:
    price = float(input("Price of item: "))
  except ValueError:
    print("Please enter a float for price of item!!")
    continue
  if price <= 0:
    print("Please enter a positive value for the price!!!")
    continue

  total = total + (Quantity*price)

  answer = input("Want to add more? (Y/N)" )
  if answer == 'Y':
    more_to_add = True
  else:
    more_to_add = False

 print(total)
于 2021-03-02T13:27:47.223 回答