-1
class Question:
    def __init__(self, prompt, answer):
        self.prompt = prompt
        self.answer = answer

I created the question_class.py file above and imported it into the quiz.py file below. I am trying to run a while loop to ask the user whether they are interested in playing the quiz again. However, the code does not run.

How do I properly insert a play again while loop? Also, how do I ask if the user is ready to play the game and properly check for input errors?

This is my first individual project and trying to learn how to code my own project after completing a beginners tutorial. I appreciate all feedback.

    from question_class import Question

    username = input("What is your name? ")
    print(f"Welcome, {username}!")

    play_again = 'y'
    while play_again == 'y':

    question_prompts = [
    ]

    questions = [
        Question(question_prompts[0], "b"),
        Question(question_prompts[1], "a"),
    ]

    def run_test(questions):
    score = 0
    for question in questions:
        answer = input(question.prompt)
        if answer == question.answer:
            score += 1
        print("You answered " + str(score) + "/" + str(len(questions)) + " correct.")

    play_again = input("Want to play again(y/n): ")

    run_test(questions)
  • 1
    A "proper" Stack Overflow question contains a single question, after appropriate research into the matter. Your three numbered items are covered in other SO questions and a variety of other on-line resources, giving much better help than we can (should) pack into a single answer here. Your request for "any constructive feedback" is too broad for this site. – Prune Feb 08 '19 at 02:07
  • 1
    I recommend that you attack your design / implementation problems individually. Write a few lines of code to implement a loop to repeat the game. These techniques should give you items 1 and 2. **Next**, you can work on reading a handful of questions from a file. After that, you get user input and compare the the right answer. Validate the user's guess. Implement the score-keeping ... and each of these is a separate programming "sprint" for you. If you get stuck on something, *then* you have a good, specific question to post here. – Prune Feb 08 '19 at 02:11

1 Answers1

0

You have a couple indentation issues. First let's get rid of the

play_again = 'y'
while play_again == 'y': 

part since it throws an error.

  1. How do I ask if the user is ready to play the game

Get the user input, and exit if the are not ready:

if input("Are you ready?") != "y": exit()
  1. How do I insert a play again while loop:

You already defined what goes inside the gameloop in your function run_test(). In your run_test() let's just return whether or whether not they want to play again:

def run_test(questions):
    score = 0
    for question in questions:
        answer = input(question.prompt)
        if answer == question.answer:
            score += 1
        print("You answered " + str(score) + "/" + str(len(questions)) + " correct.")

    return input("Want to play again(y/n): ") == 'y'

Then we can build a simple while loop:

play_again = True
while play_again:
    play_again = run_test(questions)
  1. How can I properly check for input errors?

In it's current state you don't actually need to. If the user inputs an invalid input, if answer == question.answer: will evaluate as False, so they'll just automatically get the question wrong.

Primusa
  • 13,136
  • 3
  • 33
  • 53