Bottom Page

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()
Quote
#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.
Quote
#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
Quote
#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

Quote
#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:
Quote
#6
Indentation is still incorrect.
Quote
#7
On line 9, you miss a closing parenthesis at the end of the line.
On line 10, the indent is incorrect.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  if the input is not number, let user input again teatea 14 364 Jun-02-2020, 07:03 PM
Last Post: ibutun
  Get input directly as a number? Pedroski55 4 240 May-05-2020, 04:29 PM
Last Post: deanhystad
  Guess the word game help jackthechampion 1 191 Mar-30-2020, 01:01 AM
Last Post: deanhystad
  Generate only one random number for many tries Bhavika 2 124 Mar-29-2020, 12:12 PM
Last Post: Bhavika
  Finding MINIMUM number in a random list is not working Mona 5 355 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Find Average of User Input Defined number of Scores DustinKlent 1 279 Oct-25-2019, 12:40 AM
Last Post: Larz60+
  How to recognize space or enter as one-character input? Mark17 5 346 Oct-17-2019, 08:19 PM
Last Post: jefsummers
  same number everytime when using random.randint summeringpainting 5 898 Aug-23-2019, 11:01 AM
Last Post: Malt
  help cant do random number nonzzo 1 477 Mar-28-2019, 07:33 PM
Last Post: Yoriz
  problems with random number/string PrasadPython 3 482 Mar-01-2019, 06:00 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)