I am a very new programmer in Python with only around 5 hours of experience with the beginning things. I spent around 4 hours learning the basics, and I have practiced some. I am trying to create a simulation to practice my coding skills, but I don't know how to do this, and I have tried looking it up, but there were no answers. My simulation is about multiple chefs that have different attributes like their name, recipes, and rating. For their name and recipes it is working fine as I am using strings, although the ratings I am trying to use the "import random" thing to generate a number 1 - 5 to give the chef a certain rating. Then what I want to do is use the number that is generated, which is being printed, to add to their rating, most likely in a string for simplicity. I have two different files, the "Chef" file which is the main file where all the different types of chefs will be imported from their own specific file. In my case currently, I have created one, an Italian chef.
The codes for both are: (The one I have issues with) Chef:
from Italian import Italian
import random
for x in range(1):
print(random.randint(1, 5))
if print(5):
rating = 5/5
elif print(4):
rating = 4/5
elif print(3):
rating = 3/5
elif print(2):
rating = 2/5
elif print(1):
rating = 1/5
elif print(0):
rating = 0/5
else:
print("Error")
italian_chef = Italian("Marco", "dough, cheese, pepperoni", "dough, cheese", "pasta, pasta sauce",
"meatballs, sauce", "bread", + rating)
and the other one Italian:
class Italian:
def __init__(self, name, pepperoni_pizza_recipe, cheese_pizza_recipe, pasta_recipe,
meatball_recipe, bread_stick_recipe, rating):
self.name = name
self.pepperoni_pizza_recipe = pepperoni_pizza_recipe
self.cheese_pizza_recipe = cheese_pizza_recipe
self.pasta_recipe = pasta_recipe
self.meatball_recipe = meatball_recipe
self.bread_stick_recipe = bread_stick_recipe
self.rating = rating
The Editor says:
ErrorTraceback (most recent call last):
File "C:/Users/Name/PycharmProjects/Chefs/Chef.py", line 24, in <module>
"bread", + rating)
NameError: name 'rating' is not defined
2
5
4
3
2
1
0
when ran.
Also, just a little thing, I have now fixed this, but would there be a way to save the number each time the specific program was ran, which would allow for me to average ratings together to give an overall rating in which new rating would change?
There's a couple of issues with your code. Instead of printing out the random integer you want to save it to a variable to use later.
Printing a variable doesn't store it so you want to actually save that to a variable. Then you can divide that integer by 5 for the rating.
for x in range(1):
stars = random.randint(1,5)
ratingFloat = stars/5 # will divide starts by 5 Ex 5/5 = 1.0
rating = str(stars) + "/5" # Will save a string "5/5" to rating
I think this is what you're asking. For future reference you can't do
if print(5):
# do something
This isn't a comparison in python and it doesn't allow you to do anything with the output. You should do something like in your original code
if stars == 5:
# Do something