while and if statements problem with my script? - python

So I want to print "Wrong!" every time I get the wrong input. It works but when I type
the right input it still executes the "if" statement despite "guess.capitalize() != secret_word" being false. Can you tell me what I'm doing wrong?
multiple methods is appreciated !
secret_word = "Dog"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
while guess.capitalize() != secret_word and not out_of_guesses:
if guess_count < guess_limit:
guess = input("Enter your guess: ")
guess_count += 1
print("Wrong!")
else:
out_of_guesses = True
if out_of_guesses:
print("You ran out of guesses")
else:
print("You won!")

I commented the part of your code that is causing the bug:
while guess.capitalize() != secret_word and not out_of_guesses:
if guess_count < guess_limit:
guess = input("Enter your guess: ")# Here, the user can input correct word or wrong word
guess_count += 1
print("Wrong!") # The code goes down here, regardless of what the user input
else:
out_of_guesses = True
Corrected version:
secret_word = "Dog"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
while not out_of_guesses:
if guess_count < guess_limit:
guess = input("Enter your guess: ")# Here, the user can input correct word or wrong word
if guess.lower() != secret_word.lower():
guess_count += 1
print("Wrong!") # The code goes down here, regardless of what the user input
else:
break
else:
out_of_guesses = True
if out_of_guesses:
print("You ran out of guesses")
else:
print("You won!")

HI i think i understand what you are trying to do, i have read your code and restructred it to try give you your desired output.
secret_word = "Dog"
guess_count = 0
while True:
if guess_count != 3:
guess_count +=1
guess = input("Enter your guess: ")
if guess.upper() == secret_word.upper():
print("You Won!")
break
else:
print("Wrong")
continue
else:
print("You ran out of guesses")
break
please let me know if this is not the output you are trying to get.

Related

trying to make a game where if you reach a certain number of guesses the while loop breaks

trying to make a game where if you reach a certain number of guesses the while loop breaks
secret_word = "giraffe"
guess = ""
guess_count = 0
while guess != secret_word:
guess = input("enter guess: ")
guess_count += 1
print(guess_count)
if guess == secret_word:
print("You win")
else guess_count == "4":
print("You lost!")
break
Your code is almost there, you just need to make a few changes. First, you need a break statement within your win condition so the loop stops. Second, you need to change else to elif, or simply just an if. Also, you need to compare guess_count to an int, not a str.
Code:
secret_word = "giraffe"
guess = ""
guess_count = 0
while guess != secret_word:
guess = input("enter guess: ")
guess_count += 1
print(guess_count)
if guess == secret_word:
print("You win")
break
elif guess_count == 4:
print("You lost!")
break
Output:
enter guess:
dog
1
enter guess:
dog
2
enter guess:
dog
3
enter guess:
dog
4
You lost!
I kind of reformated the code, having else with a condition didn't really make sense so I switched it to:
secret_word = "giraffe"
guess = ""
guess_count = 0
limit = 4
while guess_count < limit:
guess = input("enter guess: ")
guess_count += 1
print(guess_count)
if guess == secret_word:
print("Congrats, You Win!!")
quit()
print('Sorry, too many guesses, you lose! :(')
Good luck with the game!!

Can't enter any guesses in this guessing game made in python

I am a beginner in Python and currently learning how to code in it. I made my first guessing game in Python.
(https://i.stack.imgur.com/ZWb8u.png)
Normally, I should be able to enter the guess. But I cannot enter any guesses in it. It's prompting me that I'm out of guesses. Please help.
Original Code
guess_limit = 3
out_of_guesses = True
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 = False
if out_of_guesses:
print("Out Of Guesses, You Lose! ")
else:
print("You win!")`enter code here`
Output
C:\Users\hites\PycharmProjects\Test\venv\Scripts\python.exe C:/Users/hites/PycharmProjects/Test/app.py
Out Of Guesses, You Lose!
Process finished with exit code 0
Show the full code: we don't know what guess_count or secret_word is.
Without those, no one can really help you.
I made up guess_count and secret_word, to get the following (working) code:
guess_limit = 3
out_of_guesses = False
secret_word = 'kite' # put the secret word here
guess_count = 1
guess = input("Enter guess: ").lower()
while guess != secret_word and not (out_of_guesses):
if guess_count < guess_limit:
guess = input("Enter guess: ").lower()
guess_count += 1
else:
out_of_guesses = True
if out_of_guesses:
print("Out Of Guesses, You Lose! ")
else:
print("You win!")
That was based on what I thought you were doing in the other lines of code (the part you haven't shown), though.
So, I reconstructed your code after adding other lines. I used random.choice() for getting secret_word.
Here it is, completed:
from random import choice
guess_limit = 3
out_of_guesses = False
guess_count = 1
secret_word = choice(['password','kite','name','foul','wire','name','locker','computer','mud','wolf','fox','pliers','piano','chair','monk'])
guess = input("Enter guess: ").lower()
while guess != secret_word and not out_of_guesses:
if guess_count < guess_limit:
guess = input("Enter guess: ").lower()
guess_count += 1
out_of_guesses = False
else:
out_of_guesses = True
if out_of_guesses:
print("Out Of Guesses, You Lose! ")
else:
print("You win!")
Also, you had all your booleans mixed up, with weird parts in your code.
At this point, though, this answer is mostly based on guesswork about what you're trying to do.
Still, I hope this helps.
You could solve it this way:
guess_limit = 3
guess_count = 1
out_of_guesses = True
secret_word = "Secret Word"
guess = input("Enter guess: ")
while guess != secret_word and out_of_guesses:
if guess_count < guess_limit:
guess = input("Enter guess: ")
guess_count += 1
else:
out_of_guesses = False
if not out_of_guesses:
print("Out Of Guesses, You Lose! ")
else:
print("You win!")
Result:
Enter guess: Attempt1
Enter guess: Attempt2
Enter guess: Attempt3
Out Of Guesses, You Lose!
Enter guess: Secret Word
You win!

how to close my python loop after user tried three time

I made a loop which gives the user the chance to guess the right number. The problem is that my loop is continuing after the user guesses the right or wrong number. I want that the user can try up to three times. If they can't guess the right number within three chances then the loop will be closed.
Here is my code:
secret_number = 9
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
try:
give_number = int(float(input("give your number: ")))
if give_number == secret_number:
print("you won")
elif give_number != secret_number:
print("you guess wrong number")
except ValueError:
print("only integer or float value allowed")
you need to inclement value by 1 otherwise you loop will be continue.
secret_number = 9
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
guess_count += 1
try:
give_number = int(float(input("give your number: ")))
if give_number == secret_number:
print("you won")
elif give_number != secret_number:
print("you guess wrong number")
except ValueError:
print("only integer or float value allowed")
else:
guess_count = guess_limit
print("you tried maximum time")
secret_number = 9
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
try:
give_number = int(float(input("give your number: ")))
if give_number == secret_number:
print("you won")
break# break loop
elif give_number != secret_number:
print("you guess wrong number")
except ValueError:
print("only integer or float value allowed")
else:
guess_count += 1# increment count
First you should increase the guess_count, Doing that will make the loop automatically exit after 3 tries.
Also, you should include a break if the user guesses correctly. The break keyword exits the loop immediately.
secret_number = 9
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
try:
give_number = int(float(input("give your number: ")))
if give_number == secret_number:
print("you won")
break
elif give_number != secret_number:
print("you guess wrong number")
guess_count += 1
except ValueError:
print("only integer or float value allowed")

I am not getting desired output

import random
num = random.randint(1,10)
guess = 0
guess_count = 0
out_of_guess = False
print(num)
while guess!=num and not (out_of_guess):
if guess_count<3:
guess = input("Enter a guess : ")
guess_count += 1
else:
out_of_guess = True
if out_of_guess:
print("You Lose!")
else:
print("You Win")
Note: I wrote print(num) so that I can see if the program is working or not.
The program always prints out you lose even when I type in the right number. I get 3 chances as programmed but it does not stop after I type the correct number, say, on the second try. I can't figure what is going wrong in this program.
The problem is that you are comparing an input that is an str to num that it's int you can cast the input to int by that the guess!=num the statement will return True in case of the input number is equal to the num.
import random
num = random.randint(1,10)
guess = 0
guess_count = 0
out_of_guess = False
print(num)
while guess!=num and not (out_of_guess):
if guess_count<3:
guess = input("Enter a guess : ")
guess = int(guess) if guess.isdigit() else guess
guess_count += 1
else:
out_of_guess = True
if out_of_guess:
print("You Lose!")
else:
print("You Win")
The issue here is the type of the obects. the function randint give you a integer, while the input that you get is a string. So you have to cast the integer to string or the string to integer to fix the issue
import random
num = random.randint(1,10)
guess = 0
guess_count = 0
out_of_guess = False
print(num)
while guess!=str(num) and not (out_of_guess):
if guess_count<3:
guess = input("Enter a guess : ")
guess_count += 1
else:
out_of_guess = True
if out_of_guess:
print("You Lose!")
else:
print("You Win")
You forgot to compare guess into an integer.
Just try:
while int(guess) != num and not out_of_guess:
...
Of course, if you input a letter, it will raise an error. It's up to you to figure out how to deal with it.
the method input return a string object and you need to convert it to integer. using the int(str) method. you can enclouse it with try except block for manipulating wrong user inputs (not numbers). see below:
import random
num = random.randint(1,10)
guess = 0
guess_count = 0
out_of_guess = False
print(num)
while guess!=num and not out_of_guess:
if guess_count<3:
try:
guess = int(input("Enter a guess : "))
guess_count += 1
except ValueError:
print("Only numbers is allowed ! try again!")
else:
out_of_guess = True
if out_of_guess:
print("You Lose!")
else:
print("You Win")
Well, I think, this might be the way you are looking for:
import random
secret_num = random.randint(1,10)
guess = 0
print("Secret Number: "+ str(secret_num))
for i in range(3): # 3 chances
guess = eval(input("Enter a guess: ")) #get input
if(guess == secret_num): #if guess is right, break
break
if(guess==secret_num):
print("You won")
else:
print("You lose")
Less lines of code
Less variables
Easy to understand & read
Hope it helps :)

Python number guessing game - Cannot display a win

I'm still quite new to Python so I apologise if this is too easy or stupid, but I was recently given the task to create a number guessing game. The game has 100 numbers, numbered from 1 to 100, and will also have a dice roll numbered 1 to 6 to determine how many tries you get (e.g If the user rolls a 4, the user will get 4 turns to try and guess the number between 1 to 100). So far I managed to complete most of it, however, upon testing the program myself, when I actually get the correct answer it doesn't display a win.
import random
random_number = random.randint(1, 5)#I made the range from 1 to 5 to make my chances of guessing the correct number greater#
guessnum_dice = random.randint(1, 6)
guess_count = 0
guess_limit = guessnum_dice
out_of_guesses = False
win = False
# This name function prints out the users name; this is because the task asks me to save each game's
# statisitics and record them within an external text file. Still haven't figured out how to do that
# as well :(
def name_function():
x = input("Enter your name: ")
print("Hello, " + x)
return
name_function()
user_roll = input("Type \"roll\" to roll the dice: ")
if user_roll == "roll":
print("You have "+str(guessnum_dice) + " guesses. Use them wisely!")
if guessnum_dice == 1:
user_guess = input("Guess the secret number: ")
if user_guess != random_number:
out_of_guesses = True
if guessnum_dice == 2:
while guess_count < guess_limit:
user_guess = input("Guess the secret number: ")
guess_count += 1
if user_guess < str(random_number):
print("Higher!")
elif user_guess > str(random_number):
print("Lower")
#Here Ive tried making the user's guess equal to the number, but to no success
elif user_guess == random_number:
win = True
print("You Win")
else:
out_of_guesses = True
if guessnum_dice == 3:
while guess_count < guess_limit:
user_guess = input("Guess the secret number: ")
guess_count += 1
if user_guess < str(random_number):
print("Higher!")
elif user_guess > str(random_number):
print("Lower")
elif user_guess == random_number:
win = True
print("You Win")
else:
out_of_guesses = True
if guessnum_dice == 4:
while guess_count < guess_limit:
user_guess = input("Guess the secret number: ")
guess_count += 1
if user_guess < str(random_number):
print("Higher!")
elif user_guess > str(random_number):
print("Lower")
elif user_guess == random_number:
win = True
print("You Win")
else:
out_of_guesses = True
if guessnum_dice == 5:
while guess_count < guess_limit:
user_guess = input("Guess the secret number: ")
guess_count += 1
if user_guess < str(random_number):
print("Higher!")
elif user_guess > str(random_number):
print("Lower")
elif user_guess == random_number:
win = True
print("You Win")
else:
out_of_guesses = True
if guessnum_dice == 6:
while guess_count < guess_limit:
user_guess = input("Guess the secret number: ")
guess_count += 1
if user_guess < str(random_number):
print("Higher!")
elif user_guess > str(random_number):
print("Lower")
elif user_guess == random_number:
win = True
print("You Win")
else:
out_of_guesses = True
if out_of_guesses:
print("You Lose! The number: " + str(random_number))
if win == True:
print("You won")
And here is the output:
Enter your name: Gary
Hello, Gary
Type "roll" to roll the dice: roll
You have 6 guesses. Use them wisely!
Guess the secret number: 4
Lower
Guess the secret number: 3
Lower
Guess the secret number: 2 ### Here you can see that 2 is the correct answer, but it wont display a-
Guess the secret number: 2 ### -win no matter how many times it gets entered
Guess the secret number: 2
Guess the secret number: 1
Higher!
You Lose! The number: 2
Process finished with exit code 0
Once again I apoligise if this seems confusing as I find it difficult to try and explain my problem. Any advice would be much appreciated
Please consider using loop to go through all guesses instead of using if statements, in this way your code will be more compact and you can change the loop counter as you wish.
Also when you get user input, cast it to integer so that you can compare the numbers, otherwise user input will remain as string. check out below solution:
#This is a guess the number game
import random
print("What is your name?")
myName = input()
print("Well, " + myName + ", I am thinking of a number between 1 and 20")
loopCounter = 0
myNumber = random.randint(1, 20)
while loopCounter < 6:
print("Take a guess.")
guessNumber = input()
guessNumber = int(guessNumber)
loopCounter = loopCounter + 1
if guessNumber < myNumber:
print("Your guess is too low.")
elif guessNumber > myNumber:
print("Your guess is too high.")
else:
loopCounter = str(loopCounter)
print("Well done, " + myName + ", you guessed the number in " + loopCounter + " guesses!")
break
if guessNumber != myNumber:
myNumber = str(myNumber)
print("Nope. The number was " + myNumber)
Hope this helps.

Categories