+ 1
What are errors in this python code to build a guessing game? Could you please explain?Thanks!!
secret_word="girraffe" guess=" " guess_count=0 guess_limit=3 out_of_guesses=False while guess!=secret_word: if guess_count<=guess_limit: guess=input("enter guess:") else: out_of_guesses=True if out_of_guesses: print("you lose") else: print("you win")
5 Respuestas
+ 1
I saw quite a few possible mistakes. I've edited it and below is a working solution. Feel free to ask any questions.
https://code.sololearn.com/c628R01UvGGw/?ref=app
+ 1
So whenever user enters a right input it doesn't enters while loop and the program works fine ,but if users enter a wrong input while loop runs and then guess count is checked ,so you should increase guess_count after every input ,otherwise guess_count<guess_limit will never work and user will be able to enter any amount of wrong inputs till the right word ,also else statement will never run,
now after 3 wrong inputs if guess_count<guess_limit becomes false and enters else statement ,so you gotta put a break keyword after out_of_guesses=True to break out of while loop because even if statement won't ask for further inputs ,loop will keep running .
Here is the right code ,
secret_word="girraffe"
guess=" "
guess_count=0
guess_limit=3
out_of_guesses=False
while guess!=secret_word:
if guess_count<guess_limit:
guess=input("enter guess:")
guess_count+=1
else:
out_of_guesses=True
break
if out_of_guesses:
print("you lose")
else:
print("you win")
+ 1
In case user entered girraffe in capital words,you can even also do something like input().lower() like Benjamin did to make sure it's all small letters but that's only if want to
0
In line 8,an EOF error is shown,why is it so?
0
The code playground expects all input when you start the program. Put each guess on a new line and make sure you have put 3 guesses all at once (or put 'giraffe as one of your guesses).