random.randint seems to not work, i think - python

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? ")

Related

My guessing game not functioning correctly

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

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+

I don;t understand what is wrong with this code

Im getting a syntax error for elif option ==2:. I was wondering what I need to do to fix it. I followed the pseudocode my professor gave us but it still won't run. I'm wondering if I shouldn't have used elif or maybe something about the indentation is off.
import random
print("Welcome to the guess my number program")
while True:
print("1. You guess the number")
print("2. You type a number and see if the computer can guess it")
print("3. Exit")
option = int(input("Please enter your number here: "))
if option ==1:
#generates a random number
mynumber = random.randint(1,11)
#number of guesses
count = 1
while True:
try:
guess = int(input("Guess a number between 1 and 10:"))
while guess < 1 or guess > 10:
guess = int(input("Guess a number between 1 and 10:")) # THIS LINE HERE
except:
print("Numbers Only")
continue
#prints if the number you chose is too low and adds 1 to the counter
if guess < mynumber:
print("The number you chose is too low")
count= count+1
#prints if the number you chose is too high and adds 1 to the counter
elif guess > mynumber:
print("The number you choose is too high")
count = count+1
#If the number you chose is correct it will tell you that you guessed the number and how many attempts it took
elif guess == mynumber:
print("You guessed it in " , count , "attempts")
break
elif option == 2:
number = int(input("Please Enter a Number: "))
count = 1
while True:
randomval = random.randint(1,11)
if (number < randomval):
print("Too high")
elif (number > randomval):
print("Too low")
count = count+1
elif (number==randomval):
print("The computer guessed it in" + count + "attempts. The number was" + randomval)
break
else:
break
The problem is simple. There is no continuity between if option == 1 and elif option == 2, because of the in between while loop. What you have to do is remove the el part of elif option == 2 and just write if option == 2.
I haven't tested the whole program myself. But at a glance, this should rectify the problem.
Please comment if otherwise.

how to count the number of user inputs in Python

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

Guessing random number game python

My program is supposed to ask the user to guess a number between 0 and 100 however I can't seem to get the output right. At the moment if the the User number is greater than the random number, it prints out an infinite amount of "Your number is too high." Also if the first UserGuess is low, then all the following numbers will have the same prompt: ("Your number is too low") despite them being actually bigger than the random number. I have no idea what I am doing wrong. Any help would be greatly appreciated. Thank you!
from random import randint
def main():
guessesTaken = 0
randomNumber = randint(0,100)
#print(randomNumber)
giveUp = -1
UserGuess = int(input("Take a guess" + "(The random number is: " + str(randomNumber) + "): "))
while UserGuess != randomNumber:
guessesTaken += 1
if UserGuess < randomNumber:
UserGuess = int(input("Your guess is too high.Try again: "))
elif UserGuess > randomNumber:
UserGuess = int(input("Your guess is too high.Try again: "))
elif UserGuess == randomNumber or UserGuess == giveUp:
break
if UserGuess == randomNumber:
guessesTaken = str(guessesTaken)
print("Yes, that is right!")
print("It took you " + guessesTaken + " guesses")
if UserGuess == giveUp:
guessesTaken = str(guessesTaken)
randomNumber = str(randomNumber)
print("Better luck next time.")
print("You tried"+ guessesTaken + " guesses")
return
print (main())
if __name__ == "__main__":
main()
You need to get more user input once you're inside the loop. Consider
UserGuess = None
while UserGuess != randomNumber:
UserGuess = int(input("Take a guess" + "(The random number is: " + str(randomNumber) + "): "))
guessesTaken += 1
....
As it stands, you're taking only the first guess. Then you're repeatedly reevaluating it. Since there's no chance for the user to change their guess, it loops infinitely.
Also, you'll want to change up the order of your if/elifs.
As it stands, the structure is
if guess (is too low)
if the guess (is not too low), and (is too high)
if the guess (is not too low), and (is not too high), and (is correct) or (is 'give up')
A quick way to get it working would be
if UserGuess == randomNumber or UserGuess == giveUp:
...
elif UserGuess > randomNumber:
...
elif UserGuess < randomNumber:
....

Categories