Python number guessing game - Cannot display a win - python

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.

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

Counting Game Try/Except and calculating the users attempts

I am creating a python random counting game. I'm having some difficulties with certain parts. Can anyone here review my code? I'm having difficulty with trying to implement a try/except and a tries function that counts the user's attempts. I also have to verify that the number is a legitimate input and not a variable. So far I've gotten this far and its coming along good. I just need alittle help ironing out a few things. Thanks guys you rock.
Here is the code below:
import random
def main():
start_game()
play_again()
tries()
print("Welcome to the number guessing game")
print("I'm thinking of a number between 1 and 50")
def start_game():
secret_number = random.randint(1,50)
user_attempt_number = 1
user_guess = 0
while user_guess != secret_number and user_attempt_number < 5:
print("---Attempt", user_attempt_number)
user_input_text = input("Guess what number I am thinking of: ")
user_guess = int(user_input_text)
if user_guess > secret_number:
print("Too high")
elif user_guess < secret_number:
print("Too low")
else:
print("Right")
user_attempt_number += 1
if user_guess != secret_number:
print("You ran out of attempts. The correct number was"
+str(secret_number)+ ".")
def play_again():
while True:
play_again = input("Do you want to play again?")
if play_again == 'yes':
main()
if play_again =='no':
print("Thanks for playing")
break
def tries():
found= False
max_attempts=50
secret_number = random.randint(1, 50)
while tries <= max_attempts and not found:
user_input_text = start_game()
user_guess_count=+1
if user_input_text == secret_number:
print("It took you {} tries.".format(user_guess_count))
found = True
main()
Try this method:
def play_game():
print("Enter the upper limit for the range of numbers: ")
limit = int(input())
number = random.randint(1, limit)
print("I'm thinking of a number from 1 to " + str(limit) + "\n")
count = 1 #new line
while True:
guess = int(input("Your guess: "))
if guess < number:
print("Too low.")
elif guess > number:
print("Too high.")
elif guess == number:
print("You guessed it in " + str(count) + " tries.\n")
count = count+

On the First guess nothing is being registered even if i get it right

This is the code:
print("Welcome to my guessing game can you get the magic number hint, it's between 1 and 100 ")
import random
Magic_number = random.randrange(1, 100)
print(Magic_number)
guess = int(input("Enter your guess:"))
guess_limit = 5
guess_counter = 1
out_of_guesses = False
print("You have", str(guess_limit - guess_counter), "tries left")
while not out_of_guesses:
guess = int(input("Enter guess: "))
if guess == Magic_number:
print("Well done you got it!!")
exit(0)
elif guess < Magic_number:
print("That number is too small, try again")
elif guess > Magic_number:
print("That number is too high try again")
guess_counter += 1
print("You have", str(guess_limit - guess_counter), "tries left")
# exit clause
if guess_limit == guess_counter:
out_of_guesses = True
print("Game over, sorry")
And even if I get it correct on the first try it does something like this:
Welcome to my guessing game can you get the magic number
hint,
it's between 1 and 100
47
Enter your guess: 47
You have 4 tries left
Enter guess: 47
Well done you got it!!
As you can see even though i was correct on the first try it wasn't counted. Ps.(That is what shows up at the bottom of my screen where the code is executed.)
You do not check if guess == Magic_number after reading input on line 4.
Like this
import random
Magic_number = random.randrange(1, 100)
print(Magic_number)
guess = int(input("Enter your guess:"))
if guess == Magic_number:
print ("You won")
exit(0)
You don't need all those variables, check this.
import random
Magic_number = random.randrange(1, 100)
print(Magic_number)
guess_limit = 5
guess_counter = 0
while True:
guess = int(input("Enter guess: "))
if guess == Magic_number:
print("Well done you got it!!")
exit(0)
elif guess < Magic_number:
print("That number is too small, try again")
elif guess > Magic_number:
print("That number is too high try again")
guess_counter += 1
print(f"You have {guess_limit - guess_counter} tries left")
if guess_counter == guess_limit:
print("Game over, sorry")
exit(0)

why line 14 is printed two times

import random
random_number = random.randint(1,10) #numbers 1 - 10
guess = None
while True:
guess = input("pick a number from 1 to 10 \n")
guess = int(guess)
if guess < random_number:
print("Too low")
elif guess > 10:
print("pick a number from 1 to 10")
elif guess > random_number:
print("It's high")
else:
print("You won")
play_again = input("Do you want to play again? (y/n) ")
if play_again == "y":
random_number = random.randint(1,10) #numbers 1 - 10
guess = None
else:
print("Thank you for playing!")
break
Because You print "pick a number from 1 to 10" and then follows input label at line 8.
If you type a number greater than 10, it should print at least 3 times
guess = input("pick a number from 1 to 10 \n")
elif guess > 10:
print("pick a number from 1 to 10")
Then the loop repeats
It's not clear why you've hard-coded 10 in the if-else. Rather you can try to continue the loop
while True:
guess = input("pick a number from 1 to 10 \n")
guess = int(guess)
if guess > 10:
continue

Is there a way to limit the amount of while loops or input loops?

I was just wondering if there is a way to limit the amount of times a user can input something on a while loop. This is simply a guess a number 1-100 game. I have the found variable = False.
while not found:
user_guess = int(input("Your guess: "))
if user_guess == random_number:
print("you got it!")
found = True
elif user_guess > random_number:
print("Guess lower")
else:
print("guess higher")
I wanted to see if i could make this code seem more like a game by limiting the amount a user can guess for the input. Ive had some ideas i just cannot wrap my head around it. do i have set a variable value for the input to set the amount of times it can run? Im new to programming so i am struggling a bit.
count = 0
max_guesses_allowed = pick your max here
while not found or count < max_guesses_allowed:
user_guess = int(input("Your guess: "))
if user_guess == random_number:
print("you got it!")
found = True
elif user_guess > random_number:
count += 1
print("Guess lower")
else:
count += 1
print("guess higher")
The standard way would be to count the number of loops, and then exit if they exceed the maximum.
max_allowed = 10
attempt = 0
while not found:
attempt += 1
user_guess = int(input("Your guess: "))
if user_guess == random_number:
print("you got it!")
found = True
elif attempt == max_allowed:
print("You've reached the maximum number of guesses.")
break
elif user_guess > random_number:
print("Guess lower")
else:
print("guess higher")

Categories