2016-12-07 1 views
0

슈퍼마켓 인터페이스를 만들고 있고 돌아 가지 않고 실행하면 프로그램이 올바르게 작동합니다. 그러나 userchoice = 2이면 userchoice = 1로 돌아갈 수 없습니다. 2 또는 그 이후의 옵션으로 만 갈 수 있습니다. 내 while 루프에 문제가있을 수 있다고 생각합니다. 루프의 상단에있는 user_input을 넣는 동안단순한 while 루프 in Python

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.") 
+5

한번에 * 한 *'옵션 각각에 대한'if'와 while' 루프. –

답변

-1

당신은 무한 루프를 사용할 수 있습니다. 그렇다면 조건을 사용하면됩니다. 현재 매개 변수 당 하나의 루프를 사용 중입니다. 다른 선택을 처리하기 위해 루프를 빠져 나올 수 없기 때문에 이것은 효과가 없을 것입니다. 당신이 필요로하는 것보다

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 
+0

그냥 코드를 쓰지 않고 어쩌면 자기가 한 일에 자신을 설명 할 수 있고 OP가 잘못 되었나요? – MooingRawr

+0

@MooingRawr 이것은 내가 한 일이다. – user312016

+0

감사합니다! 이것은 많은 도움이됩니다! – jjjdada

2

당신은 더 while의를 사용하고 있습니다. 오히려 각 옵션에 대한 while보다 옵션에 대해 하나 개의 루프, 여러 if S를 사용

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