0

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?

Skinnylane
  • 13
  • 5

2 Answers2

1

The problem is that you are using print function. You need to store the choice into a variable like this:

from Italian import Italian
import random
for x in range(1):
    num = random.randint(1, 5)
    if num == 5:
        rating = 5/5
    elif num == 4:
        rating = 4/5
    elif num == 3:
        rating = 3/5
    elif num == 2:
        rating = 2/5
    elif num == 1:
        rating = 1/5
    elif num == 0:
        rating = 0/5
    else:
        print("Error")

italian_chef = Italian("Marco", "dough, cheese, pepperoni", "dough, cheese", "pasta, pasta sauce", 
"meatballs, sauce", "bread",rating)
Nimantha
  • 6,405
  • 6
  • 28
  • 69
EnriqueBet
  • 1,482
  • 2
  • 15
  • 23
1

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
David Teather
  • 568
  • 5
  • 15
  • Thanks, one question though. Is there a reason for doing == when doing things like "if stars == 5:"? – Skinnylane Apr 09 '20 at 06:20
  • @Skinnylane so that’s what’s called a comparison. You’re checking if stars is 5. It’s just how to write if statements. If you print(6 == 5) you end up with False which means if it were an if statement it wouldn’t be executed. Whatever tutorial you’re following to learn will definitely get into if statements and comparisons and if you’re not following a tutorial I recommend you find a free one on YouTube – David Teather Apr 09 '20 at 06:33
  • Oh yeah, I did learn it on my tutorial I was following, but I had forgotten it over what I had learned and was focusing on. By the way, do you know if there would 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? – Skinnylane Apr 09 '20 at 06:41
  • Here's a stack overflow post https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file-in-python – David Teather Apr 09 '20 at 19:05