mardi 3 novembre 2020

While loop not terminating on base case as expected in Python

in this programm the code was supposed to stop when i==0, and print "gameover" but instead, it is not stoping and after i==0 it is taking i==-1,-2.. and so on . how to I make it stop at I ==0.

            #no termination 
import random 
a=random.randint(0,10)
print(a)
i=5
game_over=False
num=int(input("guess the number between 0-10:\n"))
while not game_over:
    if a==num:
        print(f"congratulations {num} is correct:")
        game_over=True
    else:
        if num<a:
            print("forward")
            i-=1
            print(f"{i} choices left:\n")
            if i==0:
                print("gameover")
            else:
                num=int(input("guess again:"))
        else:
            print("backwards")
            i-=1
            print(f"{i}choices left:\n")
            if i==0:
                print("gameover")
            else:
                num=int(input("guess again:"))
                    
 
        
    
   



Aucun commentaire:

Enregistrer un commentaire