Hi so I am a total noob in programming, to strengthen my practice I tried to do a hangman project(not the typical hangman which uses GUI). PREREQUISITE Dictionaries,tuples, loops(including nesting of loops),if-else statement and the random function in python PROBLEM FACED The while loop isn't working. It runs perfect until the end, then it wont exit the while loop and and (print yikes!!you have won). It stays in the while loop(which I have set an infinite loop) and say's "enter a character" and refuses to exit the loop. Please debug my code or suggest changes in it so that I can exit the while loop after the correct word has been guessed. For ease I am only uploading the crux of the code (where I am facing the problem, feel free to run the code in your console)
import random
store=""
d=input("ENTER START: ")
p=d.lower()
sb={"fruits":("strawberry","banana","apple","orange","guava","grapes"),"colours":("red","blue","orange","brown","yellow"),"cars":("maruti","nano","ferrrari","audi","jaguar")}
keys=list(sb)
for i in range(len(keys)):
f=random.randint(0,len(keys)-1)
j=keys[f]
q=sb[j]
r=random.randint(0,len(q)-1)
x=q[r]
print(x)
if p!="start":
cc=input("Enter the CORRECT command in pls: ")
store=cc.lower()
if p=="start" or cc=="start":
print("HINT 1:ok so I have chosen",j+"!!!\n and you have",len(x)+2,"chances to guess")
for i in range(len(x)+2):
term=x
query = []
flag = 0
chance=0
while True:
query.append(input("Enter the letter: ").lower())
output = " ".join([x if x in query else "_" for x in term.lower()])
print(output)
flag += 1 if query[-1] in term.lower() else 0
chance+=1
query = list(set(query))
if flag == len(set(term)) or chance==len(term)+2:
print("YIKES!!you won!!!")
break
Aucun commentaire:
Enregistrer un commentaire