vendredi 27 novembre 2020

How to solve a malfunctioning in a life system? [duplicate]

So I made a game with a life system in Python, that everytime you get a wrong answer you loose one life, and have the opportunity to try again. It worked properly with questions wiht a single answer, but then I had the neccessity to create questions with more than one answer. Now it keeps working until you reach 0 lifes, but at that moment no matter the input it takes it as right answer and let's continue to the next question. I tried to recreate this problem in a simpler code, and the error still persists. Here's the code. If there's any way to make this code work, I would really apreciate it.

import random
Dificulty_levels={"easy" : 3, "normal" : 2, "hard" : 1}
Dificulty=input("Choose the dificulty, this will determine the amount of lvies you have (easy / normal / hard): ")
lives=Dificulty_levels[Dificulty]
num1=random.randint(1, 12)
num1_square=num1*num1 
print("What is the square root of "+str(num1_square))
answer=int(input("Your answer is: "))
while answer != num1 and answer != -num1 and lives >= 1:
    lives = lives - 1
    print("Wrong answer, you have "+str(lives)+" left")
    answer=int(input("Try again: "))
if answer==num1 or -num1:
    print("Correct!")
else:
    print("Game Over") 

Aucun commentaire:

Enregistrer un commentaire