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
#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


Messages In This Thread
RE: Guess Random Number Why i m not able to enter input - by sparkz_alot - Sep-16-2016, 04:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess the word game help jackthechampion 3 3,010 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,188 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  matrix number assignement to the random indices juniorcoder 4 1,915 Feb-19-2022, 02:18 PM
Last Post: juniorcoder
  Ask again if wrong guess with While. EduPy 4 2,243 Oct-21-2021, 07:46 PM
Last Post: menator01
  Generate random hex number ZYSIA 1 11,538 Jul-16-2021, 09:21 AM
Last Post: DeaD_EyE
  I guess it's about print tsavoSG 2 2,101 Feb-08-2021, 08:34 PM
Last Post: steve_shambles
  Generate Random operator, take user input and validate the user mapypy 4 5,533 Feb-03-2021, 08:41 PM
Last Post: nilamo
  Calling Input for Random Generation ScaledCodingWarrior 1 1,854 Feb-02-2021, 07:27 PM
Last Post: bowlofred
  Random Number Repeating Tzenesh 5 4,021 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  Random number generator charlottelol 5 3,207 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