-1

I'm trying to restart the code again after the user types "yes" in the last couple lines of code. is there a way I can do this? I'd also like to mention that I've just started python

I have tried using the while command but I'm not entirely sure how to do that. Other than that I have not tried anything else. Any help would be greatly appreciated.

score = 0
final = 0
adj = 0
adjectives = "remarkable, massive, unhappy, unusual"
name = input("Hi there whats you name?")
print("Welcome {}, this quiz is about what type of food you are. Enjoy!".format(name))

#ask the user what their favourite food is.
answer_1 = input("What is you favourite food?")
if answer_1 == "chocolate":
  print("Nice!")
   score = score + 1
elif answer_1 == "pizza":
  print("mmm pizza!")
  score = score + 2
elif answer_1 == "spaghetti":
  print("Yum!")
  score = score + 3
else:
  print("I love that!!")
  score = score + 4

#ask the user what their favourite flavour of icecream is
answer_2 = input("What is your favourite flavour of icecream?")
if answer_2 == "vanilla":
  score = score + 1
  print("Sweet!")
elif answer_2 == "chocolate":
  score = score + 2
  print("Smooth!")
elif answer_2 == "cookies and cream":
  score = score + 3
  print("Yum Yum!")
else:
  score = score + 4
  print("I like that too!")

#ask the user how many weeks ago they baked
answer_3 = float(input("How many weeks ago did you bake?"))
if answer_3 == 1:
  score = score + 1
  print("You're an expert baker!!")
elif answer_3  >= 2 and answer_3 <= 3:
  score = score +2
  print("Nice!")
elif answer_3 >= 4 and answer_3 <= 6:
  score = score + 3
  print("Thats a long time!")
else:
  score = score + 4
  print("You need to bake!")

#ask the user if they like avocado
answer_4 = input("Do you like avocado?")
if answer_4 == "yes":
  score = score + 1
  print("Nice!")
elif answer_4 == "no":
  score = score + 2
  print("Yuk!")
else:
  score = score + 3
  print("Oh really!")

#ask the user  their weight
answer_5 = float(input("How much do you weigh in Kg"))
if answer_5 <= 30  and answer_5 >= 0:
  score = score + 1
  print("Skinny!")
elif answer_5 >= 31 and answer_5 <= 45:
  score = score + 2
  print("Wow!")
elif answer_5 >= 46 and answer_5 <= 60:
  score = score + 3
  print("Thats a comfortable weight!")
elif answer_5 >= 61 and answer_5 <= 80:
  score = score + 4
  print("Thats a comfortable weight!")
elif answer_5 >= 81:
  score = score +5
  print("You need to lose some weight")


if score <=  8 and score >= 5:
  final = final + 0
elif score <= 12 and score >= 9:
  final = final + 1
elif score >= 13 and score <= 16:
  final = final + 2
elif score >= 17 and score <= 20:
  final = final + 3

if score <=  8 and score >= 5:
  adj = adj + 0
elif score <= 12 and score >= 9:
  adj = adj + 1
elif score >= 13 and score <= 16:
  adj = adj + 2
elif score >= 17 and score <= 20:
  adj = adj + 3

food = ["chocolate bar", "pizza", "spaghetti bowl", "beetroot"]
adjectives = ["a remarkable", "a massive", "an unhappy", "an unusual"]
print("You are {} {}".format(adjectives[adj], food[final]))

rating = input("How was that?")

print("Well that was fun! Thank you for taking this quiz, we hope you return {}".format(name))

again = input("would you like to start the quiz again?")
if again == "yes":

elif again == "no":
  print("Bye then!")
  exit()
else:
  print("Please enter: yes or no")
Paullyo
  • 1
  • 1
  • 1
    Easy answer: wrap the entire thing in one big while loop. Good answer: You should really be compartmentalizing your code into functions. You can then stick that function into a while loop for cleaner and more maintainable code. See https://stackoverflow.com/a/18791952 – Michael Kolber Jul 23 '19 at 01:56

1 Answers1

0

You want to wrap everything in a function, like this anytime you need some sort of functionality you can simply invoke the function and here you have it!

This is what you should do:

def runProgram():
    while(True):
      playGame() # after this is done we will ask the user if they want to go again
      if doesUserWantToPlayAgain() == True:
          continue
      else:
        break

def playGame():
    # All your code here

def doesUserWantToPlayAgain():
    yes_or_no = input("would you like to start the quiz again?")
    if yes_or_no == "yes": 
       return True
    return False

runProgram() # call this as an entry point for your program

I hope you get the idea, feel free to ask questions :)

Moshe Rabaev
  • 1,892
  • 16
  • 31