-2

I'm trying to create a game in python, a game of multiple choice called "Who wants to be a millionaire?" and the problem is I want the program to stop executing the second question once the user failed to answer the first one.

print("What is the tallest being in existence?")

input(("If you want to see the choices just press enter. " + name))

print("Is it A, Dinosaur?")
print("B, The one and only, Giraffe")
print("C,The soul of the ocean Whale")
print("or D, None")

print("So what do you believe is the answer ? " + name + " ")

answer_1 = input()

if answer_1 == Q1_answer:
    print("Correct! You Have Earned 100,000$ ")
    score = +100000
if answer_1 != Q1_answer:
    print("Im sorry, You have lost the game.")

print("Which famous inventor was born in 1856?")

print("A Einstein")
print("B, Tesla")
print("C, Napoleon")
print("D, Newton")

answer_2 = input()

if answer_2 == Q2_answer.lower():

    print("Correct! It is Tesla Indeed, Your reached  200,000$ ")
    score = +100000
else:
    print("Sorry, wrong answer. You have lost and earned 0$. ")

Georgy
  • 12,464
  • 7
  • 65
  • 73
Michael
  • 9
  • 1
  • 5
    Try using `exit()` – Mike67 Aug 23 '20 at 19:30
  • You should reshape your code. For example use a loop to run through the questions. Also you could store the questions, options and correct answer in a list, for example... – Peter Ark Aug 23 '20 at 19:38
  • Please look at while loop as an option. sys.exit() may not be the best option for you. I think you want to gracefully exit the program. https://www.w3schools.com/python/python_while_loops.asp and https://www.geeksforgeeks.org/python-while-loops/ – Joe Ferndz Aug 23 '20 at 19:40

3 Answers3

1

I think you can write your program in a better way, currently you cannot easily add questions as you will have to repeat the whole code for each new question. You can store all your questions in one list and then iterate over them. I also did not need sys.exit() because of the way I organized the code. Here is the code:

questions = [
    {
        "question": "What is the tallest being in existence?",
        "options": [
            "Is it A, Dinosaur?",
            "B, The one and only, Giraffe",
            "C,The soul of the ocean Whale",
            "or D, None"
        ],
        "correctOption": "a",
        "prize": 100_000
    },
    {
        "question": "Which famous inventor was born in 1856?",
        "options": [
            "A Einstein",
            "B, Tesla",
            "C, Napoleon",
            "D, Newton"
        ],
        "correctOption": "b",
        "prize": 100_000
    }
]

isGameWon = True
score = 0

for question in questions:
    print(question['question'])
    input("If you want to see the choices just press enter.")
    for option in question['options']:
        print(option)
    print("So what do you believe is the answer?")
    answer = input()
    if answer.lower() == question['correctOption']:
        score = score + question['prize']
        print("Correct! You Have Earned $" + str(score) + ".")
    else:
        isGameWon = False
        break

if (isGameWon):
    print("Congrats! You have won the game. You earned $" + str(score) + ".")
else:
    print("Sorry, wrong answer. You have lost and earned $0.")
Rishabh Gupta
  • 734
  • 6
  • 10
0

If you want to exit the program completely you need to call the exit method.

import sys
...    
if answer_1 != Q1_answer:
    print("Im sorry, You have lost the game.")
    sys.exit()
0

You can use exit() to exit your program. I`d print a message telling the user that the program will be exited.