Can't enter any guesses in this guessing game made in python - 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!

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!!

Wrote a very simple word guessing game in python where you are given a hint every-time you guess wrong but it does not work

secret_word = "Banana"
guesses = ""
maximum_guesses = 5
number_of_guesses = 0
out_of_guesses = False
while guesses != secret_word and not out_of_guesses:
if number_of_guesses == 0:
guesses = input("Guess a word: ")
number_of_guesses += 1
elif number_of_guesses == 1:
print("The object is edible")
guesses = input("Guess again: ")
number_of_guesses += 1
elif number_of_guesses == 2:
print("The object is yellow: ")
guesses = input("Guess again: ")
number_of_guesses += 1
elif number_of_guesses == 3:
print("The object is a fruit: ")
guesses = input("Guess again: ")
number_of_guesses += 1
elif number_of_guesses == 4:
print("You're stupid ")
guesses = input("Guess again: ")
number_of_guesses += 1
else:
out_of_guesses == True
if out_of_guesses == True:
print("You have lost")
else:
print("you have won")
The only problem is that when you guess the word correctly the loop keeps going and does not recognise that guesses = secret_word. Therefore there is no way of actually winning the game and all users are doomed to fail. I was thinking it might be an issue with how information is inputed into the guesses variable by the user and therefore the guesses variable can't be compared to the secret_word but it seems to be written fine so idk.
Your code works fine. However, one of the principles of programming is DRY - Don't repeat yourself.
Instead of writing your code like you have, try to avoid repetition. This makes your code easy to read, understand, and debug.
You can use lists to store the message to print on each turn.
secret_word = "banana"
hints = ["", "The object is edible", "The object is yellow", "The object is a fruit", "You're stupid"]
maximum_guesses = len(hints)
Then all you need to do is use the turn number to access the message from the list. You can also use a for loop to track whether the loop was broken or ended successfully.
Also, you probably want to compare the lowercase version of the word so that your program doesn't think that "banana" and "Banana" are different words.
for guess_num in range(maximum_guesses):
if guess_num == 0:
guess = input("Guess a word: ")
else: # Print the hint and guess again for turn 2 onwards
print(hints[guess_num])
guess = input("Guess again: ")
if guess.lower() == secret_word: # If correctly guessed, say so and break the loop
print("You have won")
break
else: # Loop wasn't broken, player lost
print("You have lost")
See? So much more readable and therefore easy to figure out if anything is wrong!

while and if statements problem with my script?

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.

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.

am trying to make the while loop repeats again and again by the user after it finishes

I am new in python and I am trying to make a very basic and simple guessing game , I wanted the loop to be repeated again after it had finished.... when the user type yes in the command the loop should repeat again please help me .....in line (31):
import random
password =[1,2,3,4,5,6,7,8,9,10]
guess = ""
guess_limit = 3
guess_count=0
out_of_guess=False
you_lose = True
#body of the game
while guess != password and not(out_of_guess):
if guess_count < guess_limit:
guess = input(" enter a guess : ")
guess_count += 1
else:
out_of_guess=True
if out_of_guess:
you_lose=True
print("you LOSE :( ")
# PLAYING again
while True :
answer = input("do you want to play again ??\n yes or no ?? \n ")
yes_input= "yes"
no_input= "no"
if yes_input == answer:
random.shuffle(password)
#here i want to return from the beginning to start playing again
elif no_input == answer:
break
else:
print("********INVALID INPUT********")
You can wrap the whole program in one while True loop; then when you get to the play again section, you can just restart with continue:
import random
password = [1,2,3,4,5,6,7,8,9,10]
while True :
guess = ""
guess_limit = 3
guess_count=0
out_of_guess=False
you_lose = True
while guess != password and not(out_of_guess):
if guess_count < guess_limit:
guess = input(" enter a guess : ")
guess_count += 1
else:
out_of_guess=True
if out_of_guess:
you_lose=True
print("you LOSE :( ")
answer = input("do you want to play again ??\n yes or no ?? \n ")
yes_input= "yes"
no_input= "no"
if yes_input == answer:
random.shuffle(password)
continue
elif no_input == answer:
break
else:
print("********INVALID INPUT********")
But right now, it is impossible for the password to be guessed because the password is a list and input() is returning a str. You could make a str version of the password to check against, though:
import random
password = [1,2,3,4,5,6,7,8,9,10]
while True :
str_password = ''.join(str(i) for i in password)
#so correct input is "12345678910"
guess = ""
guess_limit = 3
guess_count=0
out_of_guess=False
you_lose = True
while guess != str_password and not(out_of_guess):
#and so on
Your code doesn't even work. You compare a string to a list. I think that you want to choose random element (number) from the list, and then check if the input is equal to that random.
I organized your code and make it look more clean.
import random
pass_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
def game(guess: int) -> str:
guess_count = 0
while not guess_count <= 3:
if guess == random.choice(pass_list):
return 'You won!'
guess_count += 1
return 'You Lose'
while True:
guess = int(input('Enter a guess: '))
print(game(guess))
if input('Play again? ').lower() == 'yes':
game(guess)
else:
break
Instead of writing your code that way, you can use a function to organize your code and it will make it easier to understand.
import random
def password_guessing():
guess_count = 3
while True:
print("\nGuesses remaining: " + str(guess_count))
value = int(input("Enter a guess: "))
guess = random.choice(password)
if value == guess:
print("Correct guess!\n You won")
break
else:
guess_count -= 1
if guess_count >= 1:
pass
else:
print("You Lose :(\n")
ans = input("You want to play again?(y/n): ")
if ans == 'n':
break
# declaring it again since value in guess_count is 0 currently
guess_count = 3
password = [1,2,3,4,5,6,7,8,9,10]
print("Password Guessing game!")
answer = input("Enter 'yes' in order to play: ")
if answer == 'yes':
password_guessing()
print("*****GAME OVER*****")
else:
print("*****GAME OVER*****")

Categories