如何保存变量以供以后在“While”循环中使用?

2024-09-28 19:25:11 发布

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

我有一个我的工商管理课程的项目,我必须为一家比萨饼餐厅做一份订单。我需要让人选择比萨饼的大小,以及想要的配料。我试着做一个循环,这样你就可以订购多个比萨饼和每个比萨饼的多个配料。然而,按照我目前的方式,每次我重新运行循环“订购”另一个比萨饼时,它都会重置变量。有没有办法让我保存变量以便以后计算订单总价时使用?这个项目是用python编写的,但我们在技术上使用这个网站:https://www.jdoodle.com/python-programming-online/,因为不是班上的每个人都能使用实际的程序,因为我们在网上工作,而且有些人有Chromebook。它与Python空闲程序基本相同,但需要您键入更多的print

这是代码的主要部分:

while again == "y":
    size = input("Select Size (Small = $8, Medium = $10, Large = $12. Select a Number 1-3: ")
    print size
    if size == 1:
        pizzasize = 8
        pizzaname = "Small"
    if size == 2:
        pizzasize = 10
        pizzaname = "Medium"
    if size == 3:
        pizzasize = 12
        pizzaname = "Large"
    topprice = 0
    print
    again2 = "y"
    while again2 == "y":
        print ("Step 3: Select Toppings")
        print
        print ("Toppings: None, Extra Cheese, Pepperoni, Sausage, Bacon, Pineapple, Peppers")
        topping = input("Select a Number 1-7: ")
        print topping
        if topping == 1:
            topname = "None"
        if topping == 2:
            topname = "Extra Cheese"
        if topping == 3:
            topname = "Pepperoni"
        if topping == 4:
            topname = "Sausage"
        if topping == 5:
            topname = "Bacon"
        if topping == 6:
            topname = "Pineapple"
        if topping == 7:
            topname = "Peppers"
        print ("Would You Like Another Topping?")
        again2 = raw_input("Press Y/N: ")
        print again2
        print

        topprice += 1.25
        print topprice



    print ("Would You Like To Order Another?")
    again = raw_input("Press Y/N: ")
    print again
    if again == "y" and size ==1:
        pizzasize += 8
    if again == "y" and size ==2:
        pizzasize += 10
    if again == "y" and size ==3:
        pizzasize += 12
    print pizzasize
    print

Tags: and项目inputsizeifselectprintagain