Error in my Python guessing game. errors appearing - python

I made a Guessing game in python in which there will be a String That you will have to guess and input it. You get 3 tries and if failed a printed statement will show up saying "Game lost", But I am getting errors that I am unable to resolve. Here is the code:
Total_Guesses = 3
Guess = ""
G = 1
Seceret_Word = "Lofi"
Out_of_Guesses = False
while guess != Seceret_Word and not(Out_of_Guesses):
if G < Number_of_Guesses
guess = input("Enter your guess:")
G += 1
else:
Out_of_Guesses = True
if Out_of_Guesses:
print("OUT OF GUESSES")
else:
print("you win")

Please share your error(s) with us.
First of all there are simple syntax problems in your code. Indentation is very important concept in Python.
Indentation:
while something:
doThis()
or something like this,
if something:
doThat():
Also there are some variables you didn't define. If you don't define you can't use them. guess, Number_of_Guesses These are very important things if you are beginner.
Here is a fixed version of your code:
total_guess_count = 3
guess_count = 0
secret_word = "Lofi"
while guess_count < total_guess_count:
guess = input("Enter your guess: ")
if guess == secret_word:
print("You win!")
break
else:
guess_count += 1
if guess_count == total_guess_count:
print("You lost!")
else:
print("Try again!")
You should obey the naming variable rules to get better. Such as, generally you don't start with uppercase characther. You defined "Guess" but tried to check for "guess" in while condition.
And you should start to check winning condition first, if you try to make it in else body, program wouldn't work healthy. I mean yes it can work but probably it would be buggy.

Related

How does the 'not' keyword work in this code? I'm trying to understand if 'not' reverses the value of 'out_of_guesses'

# Create a guessing game about your name, make the user have three tries only and end the game if the user cannot guess the answer in three tries.
answer = "jay"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
while guess.lower() != answer and not out_of_guesses:
if guess_count < guess_limit:
guess = input("What's my name? ")
guess_count += 1
else:
out_of_guesses = True
if out_of_guesses:
print("You lose. ")
else:
print("You win. ")
I assigned a variable called out_of_guesses in this case; however, the while statement should use the not keyword and reverse the variable out_of_guesses to True. This isn't the case however because if the not keyword reversed the second condition to True then it would not exit the loop when out_of_guesses was True. Basically, what I'm asking is how does the while loop read the not statement? Am I misunderstanding something and how?
Perhaps you would better understand the logical expression if we inverted the sense of the boolean you have created:
...
have_more_guesses = True
while guess.lower() != answer and have_more_guesses:
if guess_count < guess_limit:
guess = input("What's my name? ")
guess_count += 1
else:
have_more_guesses = False
if have_more_guesses:
print("You win. ")
else:
print("You lose. ")
Note how I've inverted all the stages: the initialisation, the update and the final test.
This reads: "While you don't have the right answer, but you have more guesses keep guessing."

How to stop a code from executing after a specific point in python?

I am just starting out in learning python and I was trying to create a word guessing game where I have a secret word and the user has 3 attempts to guess it.
I basically finished the game but am stuck on one part of it.
Whenever I run my code, and guess the word in the first try, the terminal prints two successful print statements. For example, on line 12, I code that if the user guesses the word then print "Good Job, You win!"but whenever I guess the word in first try it also prints "You have just won" on line 24 (I coded this line so if the user guesses the word after 1st try it should print this).
SO, is there a way where I can end the code after line 12 if the condition is met?? so it does not print both messages on line 12 and 24 if guessed right in the first try.
please help this beginner noob out. Thanks!
secret_word = "Tiger"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
print("Hello, welcome to the guessing game!")
x = input("Please guess the secret word: ")
if x == secret_word:
print("Good Job, You win!")
#end the code here and not run anything after if user guesses the right word
while guess != secret_word and x != secret_word and not(out_of_guesses):
if guess_count < guess_limit:
guess = input("Wrong, enter your guess word again: ")
guess_count = guess_count + 1
else:
out_of_guesses = True
if out_of_guesses:
print("Sorry, You have run out of guesses.")
else:
print("You have just won")
You could call the built-in quit() function. If/when you modularize your code into functions, this could be done with a return call.
The quit() function will basically tell the program to immediately end and will not execute the rest of the code.
You can import sys and use sys.exit()
Some other ways

Beginners Coding Error, While Loop Triggering 2 Messages

I am a beginner that just started learning python this Monday and one of the first hurdles I am getting into is while loops. I was trying to code a number guessing game and when I enter the correct answer it will give me "Wrong Guess" and "Correct Guess" as outputs. I have been staring at this problem wondering why this is happening but I can't figure it out. Can someone explain why this is happening? Thanks in advance!
secret_number = 9
guess = ''
i = 0
while guess != 9 and i < 3:
guess = int(input("Guess Number: "))
i += 1
print('Wrong Guess!')
if guess == 9:
print('Your Correct!')
if i == 3:
print('You lost!')
You need to trace your code line by line. Even if your answer was correct, the print('Wrong Guess!') would still execute.
Instead, I would check if the answer is correct inside the loop.
Note: there's a bug in your code - you should be using secret_number variable instead of explicitly writing 9.
You should also only add i for wrong guesses to prevent saying you lost although you did it in 3 tries.
while guess != secret_number and i < 3:
guess = int(input("Guess Number: "))
if guess == secret_number :
print('Your Correct!')
else:
print('Wrong Guess!')
i += 1
You were getting both the messages together when you entered 9 as the input because the input statement and the Wrong Guess statement are both in the same while loop. Meaning once the code asks you to guess a number, it would then increment i and then print Wrong Guess. Only after doing all three it would go back and check for the conditions of the while loop. Because the conditions fail, for guess = 9 the next lines of code are implemented. which give you the Correct Guess output.
To get the desired output do this,
secret_number = 9
guess = ''
i = 0
while True:
if i < 3:
guess = int(input("Guess Number: "))
if guess == secret_number:
print("Correct Guess")
break
else:
print("Wrong Guess")
i += 1
else:
print("You've run out of lives.")
break
This is another way to get what you're looking for:
secret_number = 9
guess = ''
i = 0
while guess!= secret_number and i<3:
guess = int(input("Guess Number:"))
if guess == secret_number:
print("Your Correct!")
break
print("Wrong Guess!")
i+=1
if i == 3:
print('You Lost!')

How to make python say some text during while loop?

I'm new to python, I started learning a couple days ago. I'm learning from this youtube video on python from "freecodecamp" (https://www.youtube.com/watch?v=rfscVS0vtbw&t=6831s). In the video he was teaching the audience how to make a guessing game. In the game, the user gets 3 chances to guess the secret word. I'm on the latest python version, and I wanted to know, how I can make it so that after every incorrect guess, it tells the user how many guesses they have left. Here is the code so far:
print("You need to guess a yellow fruit!")
secret_word = "Bananas"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
while guess != secret_word and not(out_of_guesses):
if guess_count < guess_limit:
guess = input("Enter guess: ")
guess_count += 1
else:
out_of_guesses = True
if out_of_guesses:
print("Out of guesses, you lose!")
else:
print("You win!")
Any help would be appreciated, Thank you!
The easiest way to do that is to use an f string. F strings make it very easy to put variables into a string, and they are awesome. You would use something like print(f"you have {guess_limit-guess_count} tries left!!") on each iteration.
You can also do something like this with a placeholder
print("You have {0} trys left!!").format(guess_limit-guess_count)
It will replace the {0} with whatever you put into the format
You can also read about different ways of doing this from here

BEGINNER: Why is my code executing a fourth guess when the limit is set to three max?

Below code runs fine if your guesses are within the "three-guess" limit, but if you guess wrong the third time, the guess-task asks once more for some reason. This is not what I want. And even if you get it right the third time, the program finishes. What to do?
Code:
secret_number = 6 #random.randint(1, 12)
print("We are going to play a guessing game.")
guess_count = 3
guess = int(input('Try figuring out the computers number between 1 and 12: '))
while guess_count > 0:
if int(guess) != int(secret_number):
if int(guess) > int(secret_number):
guess = input('Too high. Try again: ')
guess_count = guess_count - 1
elif int(guess) < int(secret_number):
guess = input('Too low. Try again: ')
guess_count = guess_count - 1
elif int(guess) == int(secret_number):
print("THAT'S CORRECT!")
break
else:
print("Well, you tried.")
Output:
We are going to play a guessing game.
Try figuring out the computers number between 1 and 12: 12
Too high. Try again: 1
Too low. Try again: 4
Too low. Try again: 6
Well, you tried.
Each time the user guesses incorrectly in the loop, guess_count is decreased by 1 and the input function is called asking the user to guess again. So this will be done three times; but the input function is also used before the loop, giving the user one extra guess.
To fix it, you could set guess_count to 2 before the loop, so that the user gets two guesses in the loop, making three in total including the one before the loop. But it is probably better to use the input function only at the start of the loop, not before the loop and not after an incorrect guess; the way you have it at the moment, the user will be asked again after an incorrect guess even if they have no guesses left.
This should work. You are asking for input 3 times inside the while-loop and one time initially, so total 4 times. Also if the last prediction is correct, you are not checking it with the original value as you have used an elif condition. You can do it like this.
secret_number = 6 #random.randint(1, 12)
print("We are going to play a guessing game.")
guess_count = 2
correct_answer = False
guess = int(input('Try figuring out the computers number between 1 and 12: '))
while guess_count > 0:
if int(guess) != int(secret_number):
if int(guess) > int(secret_number):
guess = input('Too high. Try again: ')
guess_count = guess_count - 1
elif int(guess) < int(secret_number):
guess = input('Too low. Try again: ')
guess_count = guess_count - 1
if int(guess) == int(secret_number):
correct_answer = True
print("THAT'S CORRECT!")
break
if correct_answer == False:
print("Well, you tried.")
As others pointed out, you are giving free guess at the top because you are input the first guess. It prints Well, you tried because guess_count is 0 at the last call, satisfying else statement.
For debugging purpose, if you print guess_count in the else statement, you might be able to figure out.
Also You don't have to keep convert variable to int. The following is how I changed the code.
secret_number = 6 #random.randint(1, 12)
print("We are going to play a guessing game.")
guess_count = 3
while guess_count > 0:
guess = int(input('Try figuring out the computers number between 1 and 12: '))
if guess != secret_number:
if guess > secret_number:
print('Too high. Try again: ')
guess_count = guess_count - 1
elif guess < secret_number:
print("Too low. Try again: ")
guess_count = guess_count - 1
elif guess == secret_number:
print("THAT'S CORRECT!")
break
else:
print("Well, you tried.")

Categories