如何在python Pizza程序上修复此错误

时间:2018-10-30 02:34:42

标签: python

我制作了一个程序,您可以在其中选择所需大小的比萨饼,馅料,然后它将显示所有商品的价格。在进入收据部分之前,大多数方法都可以正常工作,在该部分一直向我显示关于整数和字符串的错误。有人可以帮我吗,任何帮助将不胜感激。 谢谢, 下毛毛雨

pizza_size = []
pizza_topping = []
pizza_size_price = []

pizza_size_input = input("What size of pizza do you want?\nSmall: 7.99$\nMedium: 9.99$\nLarge: 11.99\nEnter your desired pizza size: ")
pizza_size.append(pizza_size_input)
prompt = "\nPlease enter your desired topping for your pizza (.5$ per topping): "
prompt += "\n(Enter 'quit' when you are finished choosing your topics)"

if pizza_size == 'small':
    pizza_size_price.append('7.99')
elif pizza_size == 'medium':
    pizza_size_price.append('9.99')
elif pizza_size == 'large':
    pizza_size_price.append('11.99')


def loops():
    for pizza_topping in pizza_topping:
        print(pizza_topping + " = 50 cents")

def reciept():
        print("Sorry, thats not a valid pizza size.")
        print("Pizza Size: " + pizza_size_input)          
        print("Topping: " + loops)
        print("Subtotal: " + subtotal)
        print("Total: " + total)

while True:
    topping = input(prompt)
    pizza_topping.append(topping)
    print(len(pizza_topping))
    if topping == 'quit':
        extra_quit_that_is_not_needed = 'quit'
        pizza_topping.remove(extra_quit_that_is_not_needed)
        toppingss = len(pizza_topping)
        print("There are " + str(toppingss) + " toppings on your pizza")
        topping_price = len(pizza_topping) * 0.5
        print(loops)
        print(reciept)
        break
    else:
        print("I love " + topping.title() + "!")


subtotal = len(str(pizza_topping)) * 50 + (pizza_size_price)
total = subtotal * 0.13

程序结尾显示err0r

2 个答案:

答案 0 :(得分:1)

尝试使用完整代码:

pizza_size = []
pizza_topping = []
pizza_size_price = []

pizza_size_input = input("What size of pizza do you want?\nSmall: 7.99$\nMedium: 9.99$\nLarge: 11.99\nEnter your desired pizza size: ")
pizza_size.append(pizza_size_input)
prompt = "\nPlease enter your desired topping for your pizza (.5$ per topping): "
prompt += "\n(Enter 'quit' when you are finished choosing your topics)"

if pizza_size == 'small':
    pizza_size_price.append('7.99')
elif pizza_size == 'medium':
    pizza_size_price.append('9.99')
elif pizza_size == 'large':
    pizza_size_price.append('11.99')
while True:
    topping = input(prompt)
    pizza_topping.append(topping)
    print(len(pizza_topping))
    if topping == 'quit':
        extra_quit_that_is_not_needed = 'quit'
        pizza_topping.remove(extra_quit_that_is_not_needed)
        toppingss = len(pizza_topping)
        print("There are " + str(toppingss) + " toppings on your pizza")
        topping_price = len(pizza_topping) * 0.5
        for pizza_topping_ in pizza_topping:
            print(pizza_topping_ + " = 50 cents")
        print("Pizza Size: " + pizza_size_input)          
        print("Topping: " + ', '.join(pizza_topping))
        subtotal = len(pizza_topping) * 50 + int(next(iter(pizza_size_price),0))
        total = subtotal * 0.13
        print("Subtotal: " + str(subtotal))
        print("Total: " + str(total))       
        break
    else:
        print("I love " + topping.title() + "!")

很多东西被替换了。

示例输出:

What size of pizza do you want?
Small: 7.99$
Medium: 9.99$
Large: 11.99
Enter your desired pizza size: large

Please enter your desired topping for your pizza (.5$ per topping): 
(Enter 'quit' when you are finished choosing your topics)Honey
1
I love Honey!

Please enter your desired topping for your pizza (.5$ per topping): 
(Enter 'quit' when you are finished choosing your topics)Ham
2
I love Ham!

Please enter your desired topping for your pizza (.5$ per topping): 
(Enter 'quit' when you are finished choosing your topics)Bacon
3
I love Bacon!

Please enter your desired topping for your pizza (.5$ per topping): 
(Enter 'quit' when you are finished choosing your topics)Pineapple
4
I love Pineapple!

Please enter your desired topping for your pizza (.5$ per topping): 
(Enter 'quit' when you are finished choosing your topics)Cheese
5
I love Cheese!

Please enter your desired topping for your pizza (.5$ per topping): 
(Enter 'quit' when you are finished choosing your topics)quit
6
There are 5 toppings on your pizza
Honey = 50 cents
Ham = 50 cents
Bacon = 50 cents
Pineapple = 50 cents
Cheese = 50 cents
Pizza Size: large
Topping: Honey, Ham, Bacon, Pineapple, Cheese
Subtotal: 250
Total: 32.5

我删除了一些函数以消除错误并更改了一些类型,基本上就是这些。

答案 1 :(得分:0)

您的问题似乎是由于尝试向字符串添加int而引起的。如果要将数字连接到字符串,请确保首先将其转换为字符串,如下所示:

print("Total: " + str(total))

现在您有了整数(或浮点数)的字符串版本,可以将其与字符串连接。