Python Forum

Full Version: Guess Random Number Why i m not able to enter input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Below is my code to generate random number between 0 - 9 and checking with user input whether it is higher lower or equal
when I run the code, it is not taking input and showing error in
guessNumber = int(input("Guess a Random number between 0-9"))                                                                                                

  File "<string>", line 1
                                          

Can somebody please tell me where I m making mistake :dodgy: :s :s :s :s
#Guess Random Number

#Generate a Random number between 0 to 9



import random



turn = 0

def guessRandom():

secretNumber = random.randint(0,9)



guessNumber = int(input("Guess a Random number between 0-9"))

while secretNumber != guessNumber:

if(secretNumber > guessNumber):

input("You have Guessed the number higher than secretNumber. Guess Again!")

turn = turn + 1

elif (secretNumber < guessNumber):

input("You have guessed the number lower than secretNumber. Guess Again! ")

turn = turn + 1

if(secretNumber == guessNumber):

print("you Have Guessed it Right!")

guessRandom()
There's no whitespace in your code. That's unrelated to your error, but it's hard to help when I'm not looking at the same thing you are.
Not sure if it's the new code button that removed all the indents, but we'll see.  I've added indents and notes to your code so you can follow what I did.
#Guess Random Number

#Generate a Random number between 0 to 9

import random

turn = 0

def guessRandom():
    secretNumber = random.randint(0,9)

    return secretNumber    # Return a value for guessRandom

a_num = guessRandom()    # Get a value from guessRandom and assign it to a_num
print(a_num)    # We want to cheat and see what the number is :-)

while True:
    guessNumber = int(input("Guess a Random number between 0-9 "))
    if a_num == guessNumber:
        print("you Have Guessed it Right!")
        turn += 1    # We can simplify the term 'turn = turn + 1'
        break    # We're done so break out of the 'while' loop and go to next command
    elif a_num > guessNumber:
        # Need to change this from 'higher' to 'lower'
        input("You have Guessed the number lower than secretNumber. Press Enter to Guess Again!")
        turn = turn + 1
        continue    # Go back to start of 'while' 
    elif a_num < guessNumber:
        # Need to change this from 'lower to higher'
        input("You have guessed the number higher than secretNumber. Press Enter to Guess Again! ")
        turn = turn + 1
        continue    # Go back to start of 'while'

print("Turn = ", turn)
In the future, the is a "code" button (6th button from the right) on the format bar  :)
The full text of the error you get would also be useful. The code looks okay at first glance, but using int indiscriminately on input could easily cause errors depending on what the user types in.
you didn't indent correctly
This is how I did it but I had a bit of problems with it
guessnumber=0

import random
Mystery = random.randint(0,9)

while guessnumber==int(input("Guess a number between 0-9: ")):
    if guessnumber > Mystery:
        print('The number you input is not the mystery number, try again.')
    guessnumber = int(input("Enter a guess: ")
 if guessnumber == Mystery:
Indentation is still incorrect.
On line 9, you miss a closing parenthesis at the end of the line.
On line 10, the indent is incorrect.