Recently, I created this simple game in VS Code. However, if I try to run the code when the game is asking for input, the undefined statement (which I wrote for cases when the input is invalid) is executed only . The program does not re-run instead the else statement is executed for invalid input.
import random
inp=1
comp_won=user_won=0
print("WELCOME TO GUESS THE NUMBER!nnenter: n'0' for answern'-1' for exitnnBEST OF 10:n")
while int(inp)!=-1 and user_won!=3 and comp_won!=3:
try_remain=7
num_to_guess=random.randint(1,100)
while try_remain>1:
inp=input(f"guess the number({try_remain-1}): ")
if inp.isdigit():
if int(inp)==num_to_guess:
user_won+=1
print(f"you won this round!!!n{user_won}:{comp_won}n")
break
elif num_to_guess<int(inp)<101:
print("Hint: no. is lesser")
try_remain-=1
elif int(inp)==0:
try_remain-=6
elif int(inp)==-1 :
print(f"nno. was {num_to_guess}")
print("exiting programe...")
break
elif 0<int(inp)<num_to_guess:
print("Hint: no. is greater")
try_remain-=1
elif int(inp)<101 or int(inp)<-1:
print("input should be integer between 1-100")
else:
with open(r'prize.txt','a+') as f:
f.seek(0)
l=[li.strip() for li in f.readlines()]
if inp.upper() in l:
print(f"nno. was {num_to_guess}")
user_won+=1
print(f"you won this round!!!n{user_won}:{comp_won}n")
l.remove(inp)
with open(r"prize.txt",'w') as f:
for it in l:
f.write(f"{try_remain}n")
inp=1
break
print("input should be integer between 1-100")
else:
print(f"no. was {num_to_guess}")
comp_won=comp_won+1
print(f"you lost this round!!!n{user_won}:{comp_won}n")
else:
print(f"your score: {user_won}ncomputer score: {comp_won}n")
if user_won>comp_won and int(inp)!=-1:
print("you won the game!!!")
prize="".join(random.sample(['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'],4))
with open(r"prize.txt",'a') as f:
f.write(f"{prize}n")
print(f"use code-{prize} in next round for instant victory")
else:
print("computer won!!!")