0

here is my current code:

# Imports random
import random

def game():
    """This holds the function for the game"""
    # Sets score to 0 intially
    score = 0
    wrong = 0
    # Questions
    questions = [{"question": "What is the price of a motorcycle?",
                  "answers": ["$1000", "$5000", "$10000", "$15000"],
                  "correct": "2"},
                 {"question": "How much is this toaster?",
                  "answers": ["$2", "$5", "$7"],
                  "correct": "2"},
                 {"question": "What is the price of a dog?",
                  "answers": ["$1", "$5000", "$100", "$70"],
                  "correct": "3"},
                 {"question": "How much is this electric pooper scooper?",
                  "answers": ["$200000", "$90", "$72.99"],
                  "correct": "3"},
                 {"question": "What is the price of apple sauce?",
                  "answers": ["$.50", "$5", "$3", "$1"],
                  "correct": "4"},
                 {"question": "is this lamborghini worth $100,000?",
                  "answers": ["True", "False"],
                  "correct": "1"},
                 {"question": "What is the price of a lifesize manaquin of batman?",
                  "answers": ["$2,530", "$500", "$100", "$45"],
                  "correct": "1"},
                 {"question": "How much is this 1 night vacation in idaho?",
                  "answers": ["$400", "$1000", "$95"],
                  "correct": "3"},
                 {"question": "What is the price of a honda Accord?",
                  "answers": ["$1000", "$9500", "$6000", "$18000"],
                  "correct": "4"},
                 {"question": "is this gold plated microwave worth over $2,000?",
                  "answers": ["True", "False"],
                  "correct": "1"}]
    # Shuffles questions
    random.shuffle(questions)
    print("Welcome to the price is right!")
    # loop for questions
    for question in questions:
        print(question["question"])
        for i, choice in enumerate(question["answers"]):
            print(str(i + 1) + ". " + choice)
        answer = input("Choose an answer: ")
        if answer == question["correct"]:
            print("That is correct!")
            score = score + 1
        else:
            print("That answer is incorrect!")
            wrong = wrong + 1
    # Score + Thank you message
    print()
    print()
    print("Your total score is:", score, "right, and", wrong, "wrong.")
    print("Thanks for playing the price is right!")
    print()
    main()

def main():
    """Calls all options"""
    while True:
        print("Welcome to the Price is Right! I'm your host, Python! What would you like to start with?!")
        print()
        option = input("Play, View Credits, or Quit:")
        if option.lower() == "play":
            return game()
        elif option.lower() == "view credits":
            print("Created by: Gennaro Napolitano and Mario DeCristofaro")
        elif option.lower() == "quit":
            exit()
        else:
            False
            print()
            print("Sorry, that is not a valid input, please try again!")
            print()

# Calls main
if __name__ == '__main__':
    main()

Basically once the game is running the user has to choose the correct answer by choosing 1, 2 or 3(Or out of how many answers there are).

I want it to be able to re promt the user for input if they type an incorrect option for the question answer. E.g.

Question:

How much does this toaster cost?:
1. $2
2. $3
3. $4

Usern input:

A

Program Response:

Invalid response, please try again(Please choose "1", "2", or "3")

And then it'd relay the question and give the user another chance to re enter the answer.

Please note that the number of potential answers is different for most questions(e.g. some questions have 2 potential answers, some have 5, etc..)

So using the following code:

answer = input("Choose you answer: ")
while answer not in ['1', '2', '3']:
     print("Invalid response, please try again(Please choose '1', '2', or '3'")
     answer = input("Choose you answer: ")

Wouldn't work because its a set list of potentially 3 answers. It needs to be dyanic to the specific amount of answers in the specific question which is what Im struggling with.

Thanks for the help!

Gennaro
  • 113
  • 1
  • 11

1 Answers1

0

"Don't Repeat Yourself". This means since we already store the answer list in your dict:

for q in questions:
    # q['answers'] is the array of possible answers for each question q
    print(q['question'])
    answer = input("Choose you answer: ")
    while answer not in q['answer']:
        print("Invalid response, please try again(Please choose" + 
            ','.join('"'+ a + '"' for a in q[answer])
cowbert
  • 3,212
  • 2
  • 25
  • 34