Reputation: 13
I made a guessing game where the player has 10 HP and has to randomly guess a number between 1 and 3 for multiple rounds until they've guessed correctly a total of 3 times.
I coped my code into a notepad file and saved it as 123game.py, then right-clicked and ran it using Python 3.8. When I did this, an odd thing happened.
I wanted a win message to say "It's over! Thank you for playing." But when the player guesses right for the 3rd time, Python just shuts down.
However I did not have the problem when I simply copy/pasted my code into Python. Is it my code that's wrong? Or should I not save a notepad file as .py and tell windows to open it by default with Python 3.8? I didn't have the problem with Pycharm.
import random
HP = 10
finish = 3
correct = [0]
while finish >= 0:
value = random.randint(1, 3)
correct.append(value)
print("\nTry guessing a number 1, 2, or 3:")
answer = int(input())
if finish == 0:
print("It's over! Thank you for playing.")
break
if HP == 0:
print("You lose! Sorry.")
break
if answer > 3 or answer < 1:
print("Out of bounds")
continue
if answer == value:
print("correct")
print(f"The answer was {correct[-1]}.")
print(f"HP left: {HP}")
finish -= 1
print(f"correct answers until finish: {finish}")
else:
print("incorrect")
print(f"The answer was {correct[-1]}.")
HP -= 1
print(f"HP left: {HP}")
print(f"correct answers until finish: {finish}")
continue
Upvotes: 0
Views: 157
Reputation: 664
Add input()
at the end of the script. It waits for any type of input from the user but without prompting him/her like input(”Enter something: ”)
for example. Then the program ends (the same way it ended before, because there’s nothing else to execute) after pressing Return/Enter (so just press enter to exit the program). it’s naive, better consider the comments.
input() # or input(”Press Enter to exit”)`
Upvotes: 1