Python Forum
Guess Random Number Why i m not able to enter input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guess Random Number Why i m not able to enter input
#1
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()
Reply
#2
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.
Reply
#3
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  :)
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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:
Reply
#6
Indentation is still incorrect.
Reply
#7
On line 9, you miss a closing parenthesis at the end of the line.
On line 10, the indent is incorrect.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess the word game help jackthechampion 3 2,959 Sep-06-2023, 06:51 AM
Last Post: Pedroski55
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,013 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  matrix number assignement to the random indices juniorcoder 4 1,873 Feb-19-2022, 02:18 PM
Last Post: juniorcoder
  Ask again if wrong guess with While. EduPy 4 2,210 Oct-21-2021, 07:46 PM
Last Post: menator01
  Generate random hex number ZYSIA 1 11,319 Jul-16-2021, 09:21 AM
Last Post: DeaD_EyE
  I guess it's about print tsavoSG 2 2,075 Feb-08-2021, 08:34 PM
Last Post: steve_shambles
  Generate Random operator, take user input and validate the user mapypy 4 5,456 Feb-03-2021, 08:41 PM
Last Post: nilamo
  Calling Input for Random Generation ScaledCodingWarrior 1 1,821 Feb-02-2021, 07:27 PM
Last Post: bowlofred
  Random Number Repeating Tzenesh 5 3,926 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  Random number generator charlottelol 5 3,136 Nov-10-2020, 10:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020