Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I tried to write a guessing game in python.. I wrote everything correct but still it's not showing even if I guess correct number
secret_number = 5
guess_count = 0
guess_limit = 3
k = 0
while guess_count < guess_limit:
K = input(print("guess the number"))
guess_count = guess_count + 1
if k == secret_number:
print('you won')
else:
print('try again')
Most of your problem is in your input line.
Your code
K = input(print("guess the number"))
Problems
python is case-sensitive so K is not k. You are making another variable named K and not assigning to existing k.
you are calling print inside input, so you are printing None (which print returned). That should be input("guess the number")
input returns a string, and later in if k == secret_number, it's not really True because '5' is not 5 in python.
Solution
Seeing all the problems, that line should be
k = int(input("guess the number"))
change the line, your program will work.
working code
secret_number = 5
guess_count = 0
guess_limit = 3
k = 0
while guess_count < guess_limit:
k = int(input("guess the number"))
guess_count = guess_count + 1
if k == secret_number:
print('you won')
else:
print('try again')
Multiple problems:
Your else statment isn't correctly indented, and your K is capital.
Here is a working solution:
#secret number
sn=5
#amount of guesses
gc=0
#guess limit
gl=3
k=0
while gc<gl:
k=int(input("Guess the number"))
gc+=1
if k==sn:
print("you won :)")
break
else:
print("Try again:(")
[EDIT]
Also, try using int(input()) instead of just input for number programs like this, as it makes it a proper number.
You also don't need a print statement inside of an input.
A string inside an input is printed as if print() was called
guessname= "Elephant".capitalize()
enterguessname=""
guescount=0
guesslimit= 3
outofguess= False
while enterguessname != guessname and not outofguess:
if guescount < guesslimit:
enterguessname = input("Enter guess name: ").upper()
guescount += 1
else:
outofguess= True
if outofguess:
print("you lose and correct name is " + guessname)
else:
print("Your answer is right and correct answer is " +
guessname)
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed last month.
For right now, I have it so it will print the random number it chose. However, whether I get it wrong or right it always says I am wrong.
here is my code:
import random
amount_right = 0
number = random.randint(1, 2)
guess = input
print(number)
print(
"welcome to this number guessing game!! I am going to think of a number from 1-10 and you have to guess it! Good luck!")
input("enter your guess here! ")
if guess != number:
print("Not quite!")
amount_right -= 1
print("you have a score of ", amount_right)
else:
print("good Job!!")
amount_right += 1
print("you have a score of ",amount_right,"!")
what did I do wrong? I am using Pycharm if that helps with anything.
I tried checking my indentation, I tried switching which lines were the if and else statements (lines 13 - 21) and, I tried changing lines 18 - 21 to elif: statements
guess = int(input())
You need to convert your guess to int and there should be () in input
Also there are 2 input() in your code. One is unnecessary. This can be the code.
import random
amount_right = 0
number = random.randint(1, 2)
print(number)
print(
"welcome to this number guessing game!! I am going to think of a number from 1-10 and you have to guess it! Good luck!")
guess = int(input("enter your guess here! "))
if guess != number:
print("Not quite!")
amount_right -= 1
print("you have a score of ", amount_right)
else:
print("good Job!!")
amount_right += 1
print("you have a score of ",amount_right,"!")
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I'm trying to implement a text that says "Try again" to appear when the player guesses incorrectly. This is an extremely bare bones "game" but I started coding yesterday and I'm trying to learn all the basic functions and methods. This is the code:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit:
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
break
else:
print("You lost!")
I tried using another "else" function and another "if" function but I couldn't figure it out.
You can print 'Try again' after checking if the player guesses correctly. To avoid printing 'Try again' in the last guess, use an additional if statement:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit:
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
break
if guess_count < guess_limit: # add these two lines
print('Try again')
else:
print("You lost!")
Demo: https://replit.com/#blhsing/RaggedGleamingMenus
You can implement and ELSE statement, for continuing the loop:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit:
won = False
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
won = True
break
# If wrong, goes in here.
else:
# Just prints, and continues the loop
print('Try again')
# After the loop, if not won, prints lost.
if not won:
print("You lost!")
Welcome to Stack Exchange, sebas!
Here's how I would implement the "try again" message:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit: # this could also just be `while True:` since the `if` will always break out
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
break
else:
if guess_count == guess_limit:
print("You lost!")
break
else:
print("Try again!")
As the comment says, the while statement could actually just be an infinite loop of while True: since the if statement logic will always eventually break out of the loop.
UPDATE: I initially said that the while/else wasn't valid. Actually, it turns out that it is -- thanks, blhsing, for pointing that out. I would probably leave it out, since to me it makes the "try again" bit of the logic easier for my brain, but it's a nifty feature and could easily be used as well, like so:
secret_number = 9
guess_limit = 3
guess_count = 0
while guess_count < guess_limit: # this could also just be `while True:` since the `if` will always break out
guess = int(input("Guess:"))
guess_count += 1
if guess == secret_number:
print("You won!")
break
else:
if guess_count < guess_limit:
print("Try again!")
else:
print("You lost!")
add import random
and set secret_number = random.randint(0,9)
you don't need to add the 'else' statement at the last line
it should be just "print("You lost!")"
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!')
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.
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.")