I have been working on this code for some time. It keeps saying this:
Traceback (most recent call last):
File "N:\Computing\Meal Generator.py", line 30, in <module>
print(DaysOfTheWeek[0+counter],": ",Meals[random_meal]," and a number of ",NumberOfSides[random_meal],".")
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Here is the code which is displaying this error any help will be greatful.
import random
random.seed()
Meals=[]
SideDishes=[]
DaysOfTheWeek=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
print("1=Meal, 2=Side, 3=Finished")
option=input("What would you like to do?: ")
while option!=3:
if option=="1":
MealName=input("What meal would you like to add?: ")
NumberOfSides=input("How many sides would you like have with the meal?: ")
Meals=Meals,MealName,NumberOfSides
if option=="2":
SideName=input("What side would you like to add?: ")
SideDishes+=SideName
print("1=Meal, 2=Side, 3=Finished")
try_again=input("What else would you like to do?: ")
if try_again=="1":
option="1"
elif try_again=="2":
option="2"
else:
break
print("Printing out meals for you")
counter=1
for counter in DaysOfTheWeek:
random_meal=random.randint(0,len(Meals)-1)
random_side=random.randint(0,len(SideDishes)-1)
print(DaysOfTheWeek[0+counter],": ",Meals[random_meal]," and a number of ",NumberOfSides[random_meal],".")
print("And the the side that will be served with will be: ",SideDishes[random_side])
counter+=1
print("Thanks for using the Meal-O-Matic")
Thanks for your help. Tinymantwo
You have misunderstood how for loops work in Python.
When you do for counter in DaysOfTheWeek
, counter
takes in turn each value from DaysOfTheWeek
. That means the first time it will be "Mon", then "Tue", etc. So when you try to add it to 0, it fails with that error.
But you shouldn't be adding it to anything: that's the point. Instead, do this:
for day in DaysOfTheWeek:
...
print(day, ": ", Meals[random_meal]," and a number of ",NumberOfSides[random_meal],".")
and you don't need counter=0
or counter+=1
.