I have been trying to make a simple "quiz" program on Python.
And this is what I coded:

print("Mathematics Quiz")
question1 = "Who is president of USA?"
options1 = "a. Bharat Chauhan\nb. His dad\nc. His mom\nd. Barack Obama\n"
print(question1)
print(options1)

while True:
    response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")

    if response == "d":
        break
    else:
        print("Incorrect!!! Try again.")

        while True:
            response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")

            if response == "d":
                stop = True
                break
            else:
                print("Incorrect!!! You ran out of your attempts")
                stop = True
                break
        if stop:
            break

# DO the same for the next questions of your round (copy-paste-copy-paste).
# At the end of the round, paste the following code for the bonus question.

# Now the program will ask the user to go for the bonus question or not

while True:
    bonus = input("Would you like to give a try to the bonus question?\nHit 'y' for yes and 'n' for no.\n")

    if bonus == "y":
        print("Who invented Facebook?")
        print("a. Bharat Chauhan\nb. His dad\nc. Mark Zuckerberg\nd. Aliens")

        while True:
            response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")

            if response == "c":
                break
            else:
                print("Incorrect!!! Try again.")

            while True:
                response = input("Hit 'a', 'b', 'c' or 'd' for your answer\n")


                if response == "c":
                    stop = True
                    break
                else:
                    print("Incorrect!!! You ran out of your attempts")
                    stop = True
                    break
            if stop:
                break
        break
    elif bonus == "n":
        break
    else:
        print("INVALID INPUT!!! Only hit 'y' or 'n' for your response")

# Now do the same as done above for the next round and another bonus question.

Now this code is very long for a single question and I don't think this is the "true" programming. I don't want to copy paste it. I wondering is there any way to shorten the code using class or somehting like that?

Recommended Answers

All 5 Replies

For repetitive things use functions.

How can I do it with functions? If you can just explain it a littl bit. Maybe, give me an overview.

Do the book: How to think like a computer sientists learning with python. It is free downloadable from the interweb. It will learn you a lot, that is for shure... I have experianced that daniweb people are not too kind to nubs. I am currently working through the book. PM me if you want me to upload my copy for you..

Longtomjr

@longtomjr Thanks for the recommending the book. Check your inbox please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.