x = True
pizza = []
pizza_topping = []
while x:
pizza_topping = input("Please enter your pizza toppings(type 'quit' to when you're done): ")
if pizza_topping == 'quit':
print(f'Thank you for your order. Your pizza toppings are: {pizza}')
x = False
else:
pizza.append(pizza_topping)
del pizza_topping[0]
Dear people,
I get this error:
del pizza_topping[0] TypeError: 'str' object doesn't support item deletion
What am I doing wrong?
Edit: I changed 'break' to x = False(this was my original code). It got mixed up.
CodePudding user response:
You may have instantiated pizza_topping
as a list
, but you immediately start using it as a str
. It seems like you are trying to slam too many ideas together, and not considering them. You don't need toppings and a pizza. You just need a pizza and store the toppings in it.
Most of what you are trying to do can be done on one line. You don't need to delete anything, just keep overwriting the variable. Also, your loop should be tied to the 'quit' condition. I changed that condition, because typing 'quit' to quit is cumbersome. Just type nothing and press enter to quit.
s_msg = 'Enter your pizza topping or press return to quit: '
e_msg = 'Thank you for your order!\nYour toppings are:\t{}'
pizza = []
while (topping := input(s_msg)) != '':
pizza.append(topping)
print(e_msg.format(', '.join(pizza)))
There are more things to consider, though. What if the user quits, but wants to start over? What if the topping is not a valid topping? What if your user can't spell? How does your system know that pepperoni and peperony are the same thing? These are your real challenges.
CodePudding user response:
as @Samwise has already said (and the variable x is also not needed) perhaps try something like (which means your code was almost correct):
pizza = []
while True:
pizza_topping = input("Please enter your pizza toppings(type 'quit' to when you're done): ")
if pizza_topping == 'quit':
print(f'Thank you for your order. Your pizza toppings are: {pizza}')
break
else:
pizza.append(pizza_topping)
CodePudding user response:
There are several things going on here:
In Python strings are immutable, so you can't change their characters in place.
To remove the first character of the string you can do the following:
pizza_topping = pizza_topping[1:]
That being said, why do you want to remove the first character here?
Change
pizza_topping = []
topizza_topping = ""
. This change doesn't affect the run of the program, but as it will hold strings there is no sense in first setting it as a list.Remove
x = True
and just check ifpizza_topping
equals'quit'
in the while's condition.It is preferred to give variable meaningful names (
x
is not meaningful).Use
','.join
to print the contents ofpizza
.
Putting it all together:
pizza = []
pizza_topping = ""
while pizza_topping != "quit":
pizza_topping = input("Please enter your pizza toppings(type 'quit' to when you're done): ")
pizza.append(pizza_topping)
print("Thank you for your order. Your pizza toppings are: " ','.join(pizza))