Python中的简单while循环

2024-09-27 23:16:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个超级市场界面,我的程序工作正常,如果我不回去运行。但是,如果userchoice=2,我就不能返回userchoice=1。我只能去2或后面的选项。我想我的while循环可能有问题。你知道吗

apples = 0
mangoes = 0
print("Welcome to the CS110 Supermarket!")
print("1. Potatoes ($0.75 per potato")
print("2. Tomatoes ($1.25 per tomato")
print("3. Apples ($0.5 per apple")
print("4. Mangoes ($1.75 per mango")
print("5. Checkout")
user_choice = int(input())
total = total + potatoes*0.75 + tomatoes*1.25 + apples*0.5 + mangoes*1.75
while user_choice == 1:
    potatoes = int(input("How many potatoes?"))
    total = total + potatoes*0.75
    print("The total cost is $", total)
    print("Welcome to the CS110 Supermarket!")
    print("1. Potatoes ($0.75 per potato")
    print("2. Tomatoes ($1.25 per tomato")
    print("3. Apples ($0.5 per apple")
    print("4. Mangoes ($1.75 per mango")
    print("5. Checkout")
    user_choice = int(input())
while user_choice == 2:
    tomatoes = int(input("How many tomatoes?"))
    total = total + tomatoes*1.25
    print("The total cost is $", total)
    print("Welcome to the CS110 Supermarket!")
    print("1. Potatoes ($0.75 per potato")
    print("2. Tomatoes ($1.25 per tomato")
    print("3. Apples ($0.5 per apple")
    print("4. Mangoes ($1.75 per mango")
    print("5. Checkout")
    user_choice = int(input())
while user_choice == 3:
    apples=int(input("How many apples?"))
    total = total + apples*0.5
    print("The total cost is $", total)
    print("Welcome to the CS110 Supermarket!")
    print("1. Potatoes ($0.75 per potato")
    print("2. Tomatoes ($1.25 per tomato")
    print("3. Apples ($0.5 per apple")
    print("4. Mangoes ($1.75 per mango")
    print("5. Checkout")
    user_choice = int(input())
while user_choice == 4:
    mangoes = int(input("How many mangoes?"))
    total = total + mangoes*1.75
    print("The total cost is $",total)
    print("Welcome to the CS110 Supermarket!")
    print("1. Potatoes ($0.75 per potato")
    print("2. Tomatoes ($1.25 per tomato")
    print("3. Apples ($0.5 per apple")
    print("4. Mangoes ($1.75 per mango")
    print("5. Checkout")
    user_choice=int(input())
if user_choice == 5:
    print("The total cost is $",total)
    pay = float(input("Please enter an amount to pay for the fruits and vegetables: "))
    while pay < total:
        pay = float(input("Please enter an amount more than payment: "))
    change = pay - total
    print("Your change will be $", change)
    d5 = change // 5
    d1 = (change % 5) // 1
    quarter = ((change % 5) % 1) // 0.25
    dime = (((change % 5) % 1) % 0.25) // 0.1
    nickel = ((((change % 5) % 1) % 0.25) % 0.1) // 0.05
    penny = (((((change % 5) % 1) % 0.25) % 0.1) % 0.05) // 0.01
    print("Give the customer", d5, "$5 note,", d1, "$1 note,", quarter, "quarter,", dime, "dime,", nickel,"nickel,", penny, "penny.")

Tags: thetoinputchangeinttotalprintwelcome
2条回答

可以使用无限循环,同时将user_input放在循环的顶部。然后,你只需要使用if条件。当前每个参数使用一个循环。这是行不通的,因为你不能跳出这个循环去处理另一个选择。你知道吗

while True:
    print("Welcome to the CS110 Supermarket!")
    print("1. Potatoes ($0.75 per potato")
    print("2. Tomatoes ($1.25 per tomato")
    print("3. Apples ($0.5 per apple")
    print("4. Mangoes ($1.75 per mango")
    print("5. Checkout")

    user_choice = int(input())
    if user_choice==1:
        potatoes=int(input("How many potatoes?"))
        total=total+potatoes*0.75

    elif user_choice==2:
        tomatoes=int(input("How many tomatoes?"))
        total=total+tomatoes*1.25

    elif user_choice==3:
        apples=int(input("How many apples?"))
        total=total+apples*0.5


    elif  user_choice==4:
        mangoes=int(input("How many mangoes?"))
        total=total+mangoes*1.75


    if user_choice==5:
        print("The total cost is $",total)
        pay=float(input("please enter an amount to pay for the fruits and vegetables: "))
        while pay<total:
            pay=float(input("please enter an amount more than payment: "))
        change=pay-total
        print("your change will be $",change)
        d5 = change // 5
        d1 = (change % 5) // 1
        quarter = ((change % 5) % 1) // 0.25
        dime = (((change % 5) % 1) % 0.25) // 0.1
        nickel = ((((change % 5) % 1) % 0.25) % 0.1) // 0.05
        penny = (((((change % 5) % 1) % 0.25) % 0.1) % 0.05) // 0.01
        print("Give the customer", d5, "$5 note,", d1, "$1 note,", quarter, "quarter,", dime, "dime,", nickel,"nickel,", penny, "penny.")
        break

你使用的while比你需要的多。每个选项不使用while,而只使用一个循环,选项使用多个if

while True:
  user_choice=int(input())
  if user_choice == 1:
    ...
  if user_choice == 2:
    ...
  ...
  if user_choice == 5:
    break # exit the loop with break

相关问题 更多 >

    热门问题