Why is code printing "invalid literal for int() with base 10:" when running through mcdonalds and what does it mean?
Question:
When I run my code, it does not error out until it gets to the random.randint part of the code. I tried removing the mcdonalds part but the 2nd part does not work either. I have done this before with other programs but it errors out now, why?.
import time
one = "mcdonalds"
two = "burger_king"
three = "culvers"
four = "little_ceasers"
five = "Panda Express"
six = "Taco Bell"
print("welcome to the fast food randomizer! I choose between some of the most popular fast food restaurants in maricopa")
print("randomizing")
time.sleep(3)
Answer = random.randint(int(one), int(two), int(three), int(four), int(five), int(six))
print(f"The food place you are eating at is! ")
print(Answer)```
Answers:
You are trying to convert the string "mcdonalds" to a number. This can’t be done. Instead use a list and random.choice
. Here is your code with the applied changes:
import time
import random
restaurants = ["McDonalds","Burger King","Culvers","Little Ceasers","Panda Express","Taco Bell"]#List of all Restaurants
print("welcome to the fast food randomizer! I choose between some of the most popular fast food restaurants in Maricopa")
print("randomizing")
time.sleep(3)
Answer = random.choice(restaurants)#Picks random item of provided list
print(f"The food place you are eating at is {Answer}! ")# Correct usage of f-string
When I run my code, it does not error out until it gets to the random.randint part of the code. I tried removing the mcdonalds part but the 2nd part does not work either. I have done this before with other programs but it errors out now, why?.
import time
one = "mcdonalds"
two = "burger_king"
three = "culvers"
four = "little_ceasers"
five = "Panda Express"
six = "Taco Bell"
print("welcome to the fast food randomizer! I choose between some of the most popular fast food restaurants in maricopa")
print("randomizing")
time.sleep(3)
Answer = random.randint(int(one), int(two), int(three), int(four), int(five), int(six))
print(f"The food place you are eating at is! ")
print(Answer)```
You are trying to convert the string "mcdonalds" to a number. This can’t be done. Instead use a list and random.choice
. Here is your code with the applied changes:
import time
import random
restaurants = ["McDonalds","Burger King","Culvers","Little Ceasers","Panda Express","Taco Bell"]#List of all Restaurants
print("welcome to the fast food randomizer! I choose between some of the most popular fast food restaurants in Maricopa")
print("randomizing")
time.sleep(3)
Answer = random.choice(restaurants)#Picks random item of provided list
print(f"The food place you are eating at is {Answer}! ")# Correct usage of f-string