我该如何解决while循环问题?

时间:2018-12-18 16:21:35

标签: python

该程序的目标是将计数乘以汉堡包的值。我把计数放在了while循环中,但是每次运行程序时,它都会冻结并且不会执行。

def input1():
  menu = input("""
  hamburger--:  """)
  loop = input("Would you like to add another Food: (y/n)?")


  return menu, loop


def calc():
    menu, loop = input1()
    cost = 0
    full_menu = {'h': 1.25, 'f': .75, 's': 1.0}

    while menu in full_menu:
        cost = full_menu[menu]
        cost2 = 0
        cost2 += 1
        if loop == "y":
         menu, loop = input1()
         calc()
        if loop == "n":
         print(cost2 * cost )





def main():
  menu, loop = input1()
  calc()

main()

1 个答案:

答案 0 :(得分:0)

有多种方法可以使此功能与递归一起使用,但是由于您没有声明要使用它,因此不使用它会更简单。您可以完全跳过第一个函数,并在一个while循环中完成所有操作:

def calc():
    cost = 0
    full_menu = {'h': 1.25, 'f': .75, 's': 1.0}
    item='n/a'
    print('Hamburger: h\nFries: f\nSoda: s\n')
    while item:
        item = input("What would you like to order? (h/f/s or type 'q' to quit)").lower()
        if item in full_menu:
            cost += full_menu[item]
        else:
            break
    print("Your bill is ${}!".format(cost))
相关问题