i am a absolute beginner and practicing with this project "guessing the number" i have wrote the following code and wanna know what kind of error i have made.
import random
guess_count = 3
number = random.randint(1, 9)
while guess_count > 0:
guess = int(input("Guess: "))
guess_count -= 1
if guess == number:
print("Congrats You Won!")
guess_count = 0
else:
print("You Lose, Better luck next time.")
print('')
print(f"The correct number was {number}.")
Output comes out to be this,
Guess: 2
You Lose, Better luck next time.
The correct number was 6.
Guess: 6
Congrats You Won!
Expected output for failure :
Guess: 3
Guess: 4
Guess: 5
You Lose, Better luck next time.
Use break to end the loop when the guess is correct.
Put the else: block on the while loop. It will be executed if the loop ends without a break, which happens when they run out of guesses.
import random
guess_count = 3
number = random.randint(1, 9)
while guess_count > 0:
guess = int(input("Guess: "))
guess_count -= 1
if guess == number:
print("Congrats You Won!")
break
else:
print("You Lose, Better luck next time.")
print('')
print(f"The correct number was {number}.")
import random
guess_count = 3
number = random.randint(1, 9)
while guess_count > 0:
guess = int(input("Guess: "))
guess_count -= 1
if guess == number:
print("Congrats You Won!")
guess_count = 0
else:
print("You Lose, Better luck next time.")
print('')
print("The correct number was ", number , " .")
Another way to format the problem, using a for loop instead:
import random
guess_count = 3
number = random.randint(1, 9)
for x in range(0, guess_count):
guess = int(input("Guess: "))
if guess == number:
print("Congrats You Won!")
break # exit loop
else:
print("You Lose, Better luck next time.")
print('')
if x == guess_count - 1:
print(f"The correct number was {number}.")
Related
I am new to python and built this for practice. How come if you guess incorrectly the print function runs repeatedly. Additionally, if you make a correct guess the corresponding print function doesn't run.
import random
print("Welcome to the Guessing Game")
rand = random.randint(1, 9)
count = 0
running = True
guess = int(input("Pick a number from 1 - 9"))
while guess != rand:
count += 1
if guess > rand:
print("Too High")
running = False
elif guess == rand:
print("Winner Winner Chicken Dinner")
print("You won in", count, "tries!")
running = False
elif guess < rand:
print("Too Low")
If the number is incorrect we need to ask the player to guess again. You can use break to avoid having an infinite loop, here after 5 guesses.
The conversion of the input to integer will throw an error if the entry is not a valid number. It would be good to implement error handling.
import random
print("Welcome to the Guessing Game")
rand = random.randint(1, 9)
count = 0
running = True
guess = int(input("Pick a number from 1 - 9"))
while guess != rand:
count += 1
if guess > rand:
print("Too High")
running = False
elif guess == rand:
print("Winner Winner Chicken Dinner")
print("You won in", count, "tries!")
running = False
break
elif guess < rand:
print("Too Low")
if i >= 5:
break
guess = int(input("Try again\nPick a number from 1 - 9"))
in the first while loop, when player guesses the correct number we need to break.
Also for counting the number of rounds that player played we need a new while loop based on count.
When we use count in our code we should ask player every time that he enters the wrong answer to guess again so i used input in the while
import random
print("Welcome to the Guessing Game:")
rand = random.randint(1, 9)
count = 0
while count!=5:
guess = int(input("Pick a number from 1 - 9:"))
count += 1
if guess > rand:
print("Too High")
elif guess == rand:
print("Winner Winner Chicken Dinner")
print("You won in", count, "tries!")
break
elif guess < rand:
print("Too Low")
print('you lost')
I'm completely new to python as of 2 weeks ago. I'm trying to build a random number guessing game. If the guess is wrong, I want the system to return if the number is higher or lower but I keep getting either number is lower or higher repeated over and over. If the guess it right, it says I lost.
import random
random_number = random.randint(0,10)
guess = int
guess_count = 0
guess_limit = 5
out_of_guesses = False
while guess != random_number and not(out_of_guesses):
if guess_count < guess_limit:
guess = (int(input("Enter guess: ")))
guess_count += 1
if guess < random_number:
print("Number is higher")
if guess > random_number:
print("Number is lower")
else:
out_of_guesses = True
if out_of_guesses:
print("Out of guesses, YOU SUCK!")
else:
print("You win")
if guess_count < guess_limit:
guess = (int(input("Enter guess: ")))
guess_count += 1
if guess < random_number:
print("Number is higher")
if guess > random_number:
print("Number is lower")
else:
out_of_guesses = True
In this sample your else statement refers back to the last "if", that means that if your guess is not higher than random_number, it will always set out_of_guesses to true.
I don't want to spoil the answer since learning python is a fun experience. I would read up a bit more on control flow in python. This could be a good starting point.
Just indent the guess ... random number statements:
import random
random_number = random.randint(0,10)
guess = int
guess_count = 0
guess_limit = 5
out_of_guesses = False
while guess != random_number and not(out_of_guesses):
if guess_count < guess_limit:
guess = int(input("Enter guess: "))
guess_count += 1
if guess < random_number:
print("Number is higher")
if guess > random_number:
print("Number is lower")
else:
out_of_guesses = True
if out_of_guesses:
print("Out of guesses, YOU SUCK!")
else:
print("You win")
So, at this moment, you don't have the right identation. The else statement as it is right now will connect with if guess > random_number:
This means that if the guess is lower then random_number than out_of_guesses will become True, because the logic is the following:
if guess > random_number: # This returns False
else # This will happen if the guess is not grater then random_number
out_of_guesses = True
So your code should look like this:
import random
random_number = random.randint(0,10)
guess = int
guess_count = 0
guess_limit = 5
out_of_guesses = False
while guess != random_number and not(out_of_guesses):
if guess_count < guess_limit:
guess = (int(input("Enter guess: ")))
guess_count += 1
if guess < random_number:
print("Number is higher")
if guess > random_number:
print("Number is lower")
else:
out_of_guesses = True
if out_of_guesses:
print("Out of guesses, YOU SUCK!")
else:
print("You win")
So, im new to python and i have this challenge: I have to make the Guess the Number game, between me and computer. So this is where im at so far.
import random
the_number = random.randint(1, 4)
guess = 0
print(the_number)
while guess != the_number:
guess = int(input("Please enter a number: "))
if guess > the_number:
print("Player guess lower...\n")
elif guess < the_number:
print("Player guess higher...\n")
else:
print("Game Over! The number was", the_number,"The Player wins!")
break
guess = random.randint(1, 100)
if guess > the_number:
print("Computer guess lower...\n")
elif guess < the_number:
print("Computer guess higher...\n")
else:
print("Game Over! The number was", the_number,"The Computer wins!")
break
print("Thank you for playing")
I wanna know how to make this to not stop until one of us is right?
# Import random library
import random
# Assign random value between 1-100
secret_number = random.randint(1, 100)
# get the user guess
guess = int(input("Guess the secret number between 1-100: "))
# Loop untill guess not equal to secret number
while guess != secret_number:
if guess < secret_number:
print("Sorry, your guess is too low.")
else:
print("Sorry, your guess is too high.")
# guess the number again
guess = int(input("Guess the secret number between 1-100: "))
# This statement will execute after coming out of while loop
print("You guessed the number!")
You can do something like this to make the computer smarter.
import random
the_number = random.randint(1, 4)
guess = 0
print(the_number)
minPossible = 0
maxPossible = 100
while guess != the_number:
guess = int(input("Please enter a number: "))
if guess > the_number:
print("Player, guess lower...\n")
if guess < maxPossible:
maxPossible = guess - 1
elif guess < the_number:
print("Player, guess higher...\n")
if guess > minPossible:
minPossible = guess + 1
else:
print("Game Over! The number was", the_number, "The Player wins!")
break
guess = random.randint(minPossible, maxPossible)
if guess > the_number:
print("Computer, guess lower...\n")
maxPossible = guess - 1
elif guess < the_number:
print("Computer, guess higher...\n")
minPossible = guess + 1
else:
print("Game Over! The number was", the_number,"The Computer wins!")
print("Thank you for playing")
Your problem is the break statement at the end of the while loop. The code is iterating through the loop one time, then the break is ending the loop.
To get the desired output, you just need to tweak the code a bit, the break in the if statement for the computer is out of the loop of else, so it will stop the execution no matter what.
To counter this condition, you can move the break inside the else block. By doing this, the program will only break if either CPU or you correctly guess the right number.
One more thing i wanted to point out is you are printing the selecting number in the program, if it is a guessing game, isn't it supposed to be in the back-end of the code. By entering the printed number, you can win the game in the first iteration.
And just to point out, your program is selecting the number from (1, 4) but the computer is set to guess the number from (1, 100).
Kindly tick the answer if it is correct.
The modified code is -
import random
the_number = random.randint(1, 4)
guess = 0
while guess != the_number:
guess = int(input("Please enter a number: "))
if guess > the_number:
print("Player guess lower...\n")
elif guess < the_number:
print("Player guess higher...\n")
else:
print("Game Over! The number was", the_number,"The Player wins!")
break
guess = random.randint(1, 4)
if guess > the_number:
print("Computer guess lower...\n")
elif guess < the_number:
print("Computer guess higher...\n")
else:
print("Game Over! The number was", the_number,"The Computer wins!")
break
print("Thank you for playing")
Disclaimer I am a noob. Just started learning python. I know no computer languages, I just thought it would be nice to change my career for me and my family. I'm learning from online video on youtube. I think he is good... I still don't understand/know what he is talking about(terminology and stuff) but im trying to follow along and hoping I will understand wth he is talking about after hearing him say the words over and over again....
The problem is, if I win, it still says that I lose... If I lose it doesn't say that I WON... so what do I have to do differently?
I tried … if guess =/= secret_number: but apparently that / doesn't mean not equal to...
My code -
secret_number = 9
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
guess = int(input('Guess: '))
guess_count += 1
if guess == secret_number:
print('You Won!')
else:
print('You Lose!')
secret_number = 9
guess_count = 0
guess_limit = 3
while guess_count < guess_limit:
guess = int(input('Guess: '))
guess_count += 1
if guess == secret_number:
print('You Won!')
break
else:
print('You Lose!')
your code is right. I guess your problem is the indentation of if statement. you need to have if in your while loop so every time you get a number you can check if it's right or not
secret_number = 9
guess_count = 0
guess_limit = 3
status = False
while guess_count < guess_limit:
guess = int(input('Guess: '))
guess_count += 1
if guess == secret_number:
print('You Won!')
status = True
break
if status == False:
print('You Lose!')
I'm having an issue simply with print("Out of Guesses!\nYou Lose, Try Again!"). Whenever, the player runs out of guesses it prints out all print print statements twice here's the output:
Too Low!
Out of Guesses!
You Lose, Try Again!
Too Low!
When it should just print:
Out of Guesses!
You Lose, Try Again!
Code:
import random
number = random.randint(0,20)
player = ""
guess_count = 0
guess_limit = 5
out_guesses = False
while player != number and not out_guesses:
if guess_count < guess_limit:
player = int(input("Input Your Guess: "))
guess_count += 1
else:
out_guesses = True
print("Out of Guesses!\nYou Lose, Try Again!")
if player == number:
print("You Win!")
if player < number:
print("Too Low!")
if player > number:
print("Too High!")
In out_guesses scenario :
if player == number:
print("You Win!")
if player < number:
print("Too Low!")
if player > number:
print("Too High!")
This code block is getting executed twice instead of one. You've to stop it by logic. Here is a sample :
import random
number = random.randint(0, 20)
player = ""
guess_count = 0
guess_limit = 5
while player != number:
if guess_count < guess_limit:
player = int(input("Input Your Guess: "))
guess_count += 1
if player == number:
print("You Win!")
if player < number:
print("Too Low!")
if player > number:
print("Too High!")
else:
print("Out of Guesses!\nYou Lose, Try Again!")
break