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.

Thanks for the help!

Gennaro
  • 113
  • 1
  • 11
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Reti43 Oct 23 '17 at 22:53

3 Answers3

0

Gennaro,

put your input instruction in a loop, something like:

while true:
    answer = input("Choose an answer: ")
    if int(answer) in [1,2,3]:
        break
    else:
        print("risposta non valida ;-)")

Cheers Charlie

EDIT:

If the number of answers is not fixed as you report in your comments just do this:

while true:
    answer = input("Choose an answer: ")
    if int(answer) in question['answers']:
        break
    else:
        print("risposta non valida ;-)")
Charlie
  • 1,750
  • 15
  • 20
  • Thanks for the response! Unfortunately, this doesn't solve the entire issue, as I have questions that have different amounts of answers, not just 3 specifically. Some of have 5, some have 2, etc... This would work if each question only had 3 possible answers. It needs to be a bit more dynamic which is what I don't know how to do. Any other suggestions you may have so That I'm able to achieve this? Thanks! – Gennaro Oct 25 '17 at 18:27
  • This is fairly easy to work out: just look at the edits in my answers – Charlie Oct 27 '17 at 08:32
0

You just need to put the question part in a loop which will exit if the answer is correct:

# loop for questions 
for question in questions: 
    print(question["question"]) 
    for i, choice in enumerate(question["answers"]):
        print(str(i + 1) + ". " + choice)
    answer = None
    while(answer != question["correct"]):
        if answer is not None:
             print("That answer is incorrect!")
             print("Try again")
             wrong = wrong + 1
        answer = input("Choose an answer: ")          
     print("That is correct!") 
     score = score + 1
# Score + Thank you message

Edit: it appears that I misunderstood the question.

The answer is more something like (to replace your simple answer = input()):

answer = 0
while(answer < 1 or answer > len(question["answers"]):
    answer = int(input("Choose an answer: "))
fievel
  • 480
  • 3
  • 9
  • Hello Fievel, I think my question was a bit unclear. Though this does work, it's not what I was exactly going for. I just want it to re-ask the question if they put an answer in that is invalid, not incorrect. Lets say they put in the answer "1" instead of the answer being "2" that'd be incorrect, but not invalid. But if they put in the answer "a" that would be invalid due to it not being one of the possible answers. Any other suggestions on how I'm able to achieve this? – Gennaro Oct 25 '17 at 18:24
0

Before checking if the player answer is correct you can do something like this:

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: ")
  • Thanks for the response! Unfortunately this doesn't solve the entire issue, as I have questions that have different amounts of answers, not just 3 specifically. Some of have 5, some have 2, etc... This would work if each question only had 3 possible answers. It needs to be a bit more dynamic which is what I don't know how to do. Any other suggestions you may have so That I'm able to achieve this? Thanks! – Gennaro Oct 25 '17 at 18:26