Im having loads of trouble trying to count the number of guesses in this after you find the number.
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")
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")
return
Try this:
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+1 #new line
start with a variable to store the number of guesses
...
count = 0
...
then increment it on every guess
...
guess = int(input("Your guess: "))
count += 1
...
You should initialize count as 1 and increment on each loop.
import random
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
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")
return
count+=1
play_game()
An example output:
Enter the upper limit for the range of numbers:
10
I'm thinking of a number from 1 to 10
Your guess: 3
Too low.
Your guess: 7
Too low.
Your guess: 9
Too low.
Your guess: 10
You guessed it in 4 tries.
import random
highest = 10
answer = random.randrange(1,highest)
guess = 0
count = 0
print("please guess a number between 1 and {}".format(highest))
while guess != answer:
guess = int(input())
if count == 4:
exit(print("you exceeded number of chances"))
if guess == answer:
print("well done you have guessed it correctly and the answer is
{}".format(guess))
break
else:
if guess < answer:
print("please guess higher")
else:
print("please guess lower")
count = count +1
Related
It's not selecting the level of difficulty. Did I forget something? For example, I want it to select 3 for hard, which then leads the game to select from 1 to 1000 with 5 tries. and if the user selects 2 for easy they have to guess from 1 to 500 with 10 tries
import random
print('What level would you like to play 1 for easy, 2 for medium, 3 for hard')
print(1,2,3)
number = random.randint(1, 100)
number_of_guesses = 0
print(' Try Guessing a number between 1 and 100:')
while number_of_guesses < 15:
number = random.randint(1, 100)
guess = int(input())
number_of_guesses += 1
if guess < number:
print('Your guess is too low')
if guess > number:
print('Your guess is too high')
if guess == number:
break
while number_of_guesses < 10:
number = random.randint(1, 500)
guess = int(input())
number_of_guesses += 1
if guess < number:
print('Your guess is too low')
if guess > number:
print('Your guess is too high')
if guess == number:
break
while number_of_guesses < 5:
number = random.randint(1, 500)
guess = int(input())
number_of_guesses += 1
if guess < number:
print('Your guess is too low')
if guess > number:
print('Your guess is too high')
if guess == number:
break
if guess == number:
print('You guessed the number in ' + str(number_of_guesses) + ' tries!')
else:
print('You did not guess the number, The number was ' + str(number))
level = input("Print your level here")
if int(level) == 1:
# set level to 1
if int(level) == 2:
# set level to 2
if int(level) == 3:
# set level to 3
The problem with your code is that you
Don't take user input.
Even if you had user input you don't act on it.
You also aren't excepting input for the guess.
Here is a link to learn more on how to use the python input() function
https://www.w3schools.com/python/ref_func_input.asp
You forgot the user input for difficulty. Choose maximum guess and highest limit of number based on input as well. Then a single while loop can do the job.
import random
difficulty = int(input('What level would you like to play 1 for easy, 2 for medium, 3 for hard: '))
max_guess = 0
highest_number = 0
if difficulty == 1:
max_guess = 15
highest_number = 100
elif difficulty == 2:
max_guess = 10
highest_number = 500
elif difficulty == 3:
max_guess = 15
highest_number = 100
else:
print("Wrong choice")
number_of_guesses = 0
number = random.randint(1, highest_number)
while number_of_guesses < max_guess:
guess = int(input())
number_of_guesses += 1
if guess < number:
print('Your guess is too low')
if guess > number:
print('Your guess is too high')
if guess == number:
break
if guess == number:
print('You guessed the number in ' + str(number_of_guesses) + ' tries!')
else:
print('You did not guess the number, The number was ' + str(number))
Edit: Sorry previous code had some issues. here is the full code that can work
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+
i was doing a Guess the number "project" and I got stuck, when i run it, it ask me to chose a number then nothing happends, here's the code:
import random
play_game = "y"
while (play_game == "y"):
answer = random.randint(1, 100)
try_number = input("Guess a number between 1 and 100: ")
try_number = int(try_number)
counter = 1
while try_number != answer:
if try_number > answer:
print("Your number is too large")
if try_number < answer:
print("Your number is to small")
try_number = int(input("Guess a number between 1 and 100: "))
counter = counter + 1
print("You got it! You tried " + str(counter) + "times")
play_game = input("Continue? ")
Thank you for your time!
Perhaps you're running this on Python 2 instead of Python 3? On Python 2, you need to replace "input" with "raw_input", otherwise it will try to eval() the contents of the input, which is not what you want.
Because you mentioned python 3 in your tags, I'm assuming this is python 3 (Use raw_input for python 2). You have to press enter after inputting your number from 1 to 100. When i tried your code, it worked no problem.
import random
play_game = "y"
while (play_game == "y"):
answer = random.randint(1, 100)
try_number = input("Guess a number between 1 and 100: ")
try_number = int(try_number)
counter = 1
while try_number != answer:
if try_number > answer:
print("Your number is too large")
if try_number < answer:
print("Your number is to small")
try_number = int(input("Guess a number between 1 and 100: "))
counter = counter + 1
print("You got it! You tried " + str(counter) + "times")
play_game = input("Continue? ")
import random
print("Welcome to the game, Guess a Number! \n")
print("There will be 3 rounds to the game.")
print("During the first round, you will be asked to choose a number between 0-10.")
print("You will have 3 chances to guess the correct number. If you can't, the game will start over.")
print("Good Luck! \n")
def ReadyToPlay():
NumberGuesses = 0 #sets numerb of guesses
MaxGuesses = 3 #sets maximum times you can guess
number = random.randint(0,10)
print("Hi stranger, I am thinking of a number between 0 and 10.")
while NumberGuesses < MaxGuesses:
guess = int(input("Take a guess: ")) #prompt to take guess
NumberGuesses = NumberGuesses + 1 #keeps number count
if guess < number:
print("Sorry, your guess is to low.")
elif guess > number:
print("Sorry, your guess is to high.")
if guess == number: #stops current loop and continues the rest of my statment
break
if guess == number:
NumberGuesses = str(NumberGuesses)
print('\n')
print("Congrats Stranger! You have guessed the correct number.")
print("Moving on to round two. \n")
NumberGuesses = 0 #sets numerb of guesses
MaxGuesses = 3 #sets maximum times you can guess
number = random.randint(0,50)
print("Now, I am thinking of a number between 0 and 50.")
while NumberGuesses < MaxGuesses:
guess = int(input("Take a guess: ")) #prompt to take guess
NumberGuesses = NumberGuesses + 1 #keeps number count
if guess < number:
print("Sorry, your guess is to low.")
elif guess > number:
print("Sorry, your guess is to high.")
if guess == number:
break
if guess == number:
NumberGuesses = str(NumberGuesses)
print('\n')
print("You're on fire!")
print("Now you have arrived to the last round.")
print("You will have to choose 2 numbers, in order to complete the game. \n")
NumberGuesses = 0 #sets numerb of guesses
MaxGuesses = 6 #sets maximum times you can guess
number = random.randint(0,100)
print("The first number I am thinking of is between 0 and 100")
while NumberGuesses < MaxGuesses:
guess = int(input("Take a guess: "))
NumeberGuesses = NumberGuesses + 1
if guess < number:
print("Sorry, your guess is to low.")
elif guess > number:
print("Sorry, your guess is to high.")
if guess == number:
break
if guess == number:
print('\n')
print("Your guessA is correct.")
print("Now, guess the second number.")
NumberGuesses = 0 #sets numerb of guesses
MaxGuesses = 6 #sets maximum times you can guess
number = random.randint(0,100)
print("The second number I am thinking of is between 0 and 100")
while NumberGuesses < MaxGuesses:
guess = int(input("Take a guess: ")) #prompt to take guess
NumberGuesses = NumberGuesses + 1 #keeps number count
if guess < number:
print("Sorry, your guess is to low.")
elif guess > number:
print("Sorry, your guess is to high.")
if guess == number:
break
if guess == number:
print('\n')
print("Your guessB is also correct.")
print("Congratulations on finishing the game!!!")
if guess != number:
number = str(number)
print("\n")
print("The number I was thinking of was: " + number)
replay = input("Do you want to play again (y/n)?: ")
if replay == "y":
ReadyToPlay()
ReadyToPlay
i need help creating 3 levels, being able to replay the game from each level and keeping track of my wins and losses. i am a beginner so it needs to be very easy to code. So far I have created three parts to the game which all work. But when I need to reply, the game starts from the beginning and not from where u have reached up until
Use a container that will hold the state of the game; update the state during game play. Add a parameter to ReadyToPlay() that is optional. The parameter accepts a container that holds the current game state. If passed when the function is called, use it to start the game from there. Pass the state container for a replay.
You might want to consider having your function return the guess and current state then move the if guess != number: code outside of the function.
I have the following code for a random number guessing game:
import random
number = random.randint(1,100)
name = input('Hi, Whats your name?')
print ("Well", name, "i am thinking of a number between 1 and 100, take a guess")
guess1 = input()
if guess1 == number:
print ("Good job, you got it!")
while guess1 != number:
if guess1 > number:
print ('your guess is too high')
if guess1 < number:
print ('your guess is too low')
which throws the error that > or < cannot be used between str and int.
What should I do so it doesn't trigger that error?
There are two errors in your code.
You need to convert the input for guess1 from a string (by default) to an integer before you can compare it to the number (an integer).
The while loop will never stop since you are not letting the user input another value.
Try this:
import random
number = random.randint(1,100)
name = input('Hi, Whats your name?')
print ("Well", name, "i am thinking of a number between 1 and 100, take a guess")
guess1 = int(input()) # convert input from string to integer
while guess1 != number:
if guess1 > number:
print ('your guess is too high. Try again.')
elif guess1 < number:
print ('your guess is too low. Try again.')
guess1 = int(input()) # asks user to take another guess
print("Good job, you got it!")
You can make use of a while loop here - https://www.tutorialspoint.com/python/python_while_loop.htm
The logic should be:
answer_is_correct = False
while not answer_is_correct :
Keep receiving input until answer is correct
I hope this works for you:
import random
myname = input('Hello, what is your name?')
print('Well',myname,'am thinking of a number between 1 and 100')
number = random.randint(1,100)
guess = 0
while guess < 4:
guess_number = int(input('Enter a number:'))
guess += 1
if guess_number < number:
print('Your guess is to low')
if guess_number > number:
print('Your guess is to high')
if guess_number == number:
print('Your guess is correct the number is',number)
break
if guess == 4:
break
print('The number i was thinking of is',number)
from random import randint
print("you wanna guess a number between A to B and time of guess:")
A = int(input("A:"))
B = int(input("B:"))
time = int(input("time:"))
x = randint(1, 10)
print(x)
while time != 0:
num = int(input("Enter: "))
time -= 1
if num == x:
print("BLA BLA BLA")
break
print("NOPE !")
if time == 0:
print("game over")
break
Code in Python 3 for guessing game:
import random
def guessGame():
while True:
while True:
try:
low, high = map(int,input("Enter a lower number and a higher numer for your game.").split())
break
except ValueError:
print("Enter valid numbers please.")
if low > high:
print("The lower number can't be greater then the higher number.")
elif low+10 >= high:
print("At least lower number must be 10 less then the higher number")
else:
break
find_me = random.randint(low,high)
print("You have 6 chances to find the number...")
chances = 6
flag = 0
while chances:
chances-=1
guess = int(input("Enter your guess : "))
if guess<high and guess>low:
if guess < find_me:
print("The number you have entered a number less then the predicted number.",end="//")
print("{0} chances left.".format(chances))
elif guess > find_me:
print("The number you have entered a number greater then the predicted number.",end="//")
print("{0} chances left.".format(chances))
else:
print("Congrats!! you have succesfully guessed the right answer.")
return
else:
print("You must not input number out of range")
print("{0} chances left.".format(chances))
print("The predicted number was {0}".format(find_me))
You can conditions within the while loop to check if its right. It can be done the following way.
import random
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 100)
print('Well, ' + myName + ', I am thinking of a number between 1 and 100.')
inputNumber = int(raw_input('Enter the number')
while inputNumber != number:
print "Sorry wrong number , try again"
inputNumber = int(raw_input('Enter the number')
print "Congrats!"
from random import *
def play_game():
print("Let's play a number guessing game")
# Selecting a random number between 1 and 100
number = randint(1, 100)
choice = int(input("I am thinking of a number between 1 and 100. Could you guess what it is? "))
# Guide the user towards the right guess
# Loop will continue until user guesses the right number
while choice != number:
if choice < number:
choice = int(input("Too low. Can you try again? "))
elif choice > number:
choice = int(input("Too high. Can you try again? "))
continue_game = input("You guessed it right! Would you like to play it again? (Y/N) ")
# Restart the game if user wishes to play again
if continue_game == "Y":
print("--" * 42)
play_game()
else:
print("Thanks for playing :)")
exit(0)
play_game()