2013-11-11 4 views
-2

Я изменил значение шага, но программа продолжает снова и снова вводить мой сэндвич-вход. Предполагается изменить значение шага так, чтобы программа могла выйти из первого цикла while и ввести второй цикл while, но по какой-то причине первые циклы продолжают повторяться.Хотя цикл в python сохраняет повторы, несмотря на удовлетворительное условие

def main(): 
    order = 0 
    step = 0 
    total = 0 
    while step == 0: 
     print ("Welcome to Jeremy's Meat Haven, please pick one drink, one salad, and one sandwitch.") 
     print ("Please select a sandwitch by inputting 1, 2, or 3") 
     print ("(1) Hamburger -$1.00") # Print the first option on the menu, and its price, for the user 
     print ("(2) Cheeseburger -$1.50") # Print the second option on the menu, and its price, for the user 
     print ("(3) Lambburger -$2.00") # Print the third option on the menu, and its price, for the user 

     order = input("What would you like to order? (enter number): ") # Prompt the user for the number on the menu of the item they want to order 

     if order == 1: 
      total = total + 1 
      step = step + 1 

     elif order == 2: 
      total = total + 1.5 
      step = step + 1 

     elif order == 3: 
      total = total + 2 
      step = step + 1 

     elif order != 1 or 2 or 3: 
      print ("please enter a valid value of 1, 2, or 3") 

     while step == 1: 
     print ("Please select a drink by inputting 1, 2, or 3") 

     print ("(1) milkshake -$1.00") # Print the first option on the menu, and its price, for the user 

     print ("(2) coke -$1.00") # Print the second option on the menu, and its price, for the user 

     print ("(1) lemonade -$1.00") # Print the third option on the menu, and its price, for the user 



    main() 
+2

В этом отступлении определенно что-то не так, вы смешиваете вкладки и пробелы? –

+1

Вы ошибаетесь в табуляторе –

+1

Да, ваше отступы - это немного беспорядок, поэтому трудно сказать, является ли это преднамеренным или плохим вырезом и пастой. Пожалуйста, вы можете исправить? – Kev

ответ

2

Когда вы получите номер пункта здесь:

order = input("What would you like to order? (enter number): ") # Prompt the user for the number on the menu of the item they want to order 

порядок является строкой. Затем протестировать его против целого:

if order == 1: # Not going to be True since order will be '1', '2' or '3' 

Таким образом, тест на строку вместо:

if order == '1': 

или сделать заказ ИНТ:

order = int(...) 

Кроме того, вы не видите печатная ошибка об отсутствии входа 1, 2 или 3, потому что ваша логическая заявка нуждается в работе:

elif order != 1 or 2 or 3: 

Это будет стоить True, потому что if 2 и if 3 оба являются Истинными. Попробуйте:

elif order not in ('1', '2', '3')

+0

Спасибо, я полностью забыл об этом. – user2980603

0

Вы должны изменить значение шага в секунду в то время как заявление. Прямо сейчас он будет зависеть навсегда, когда вы его введете.

while step == 1: 

    print ("Please select a drink by inputting 1, 2, or 3") 

    print ("(1) milkshake -$1.00") # Print the first option on the menu, and its price, for the user 

    print ("(2) coke -$1.00") # Print the second option on the menu, and its price, for the user 

    print ("(1) lemonade -$1.00") # Print the third option on the menu, and its price, for the user 

    #ask the user to do something and change the value of step 

Кроме того, вы можете изменить

step = step + 1 

в

step += 1 

, который делает то же самое, но легче читать и является более вещий.

Смежные вопросы