0

I am working on a guess-the-4-digit-number game in python 2.7.10. But I cannot find how to randomly make a 4 digit number, and store it as a variable. The digit has to be somewhere between 1111 and 9999 The hard part is, that I want to store it as a variable, and not print it out for the player to see.

If you want to, I'll post the code when I'm finished.

The code is finally here!

import random
number = random.randint(1111,9999)
tryagain = 'yes'
print ('hello! ')
print ('------------------------------------------------------------- ')
print ('Try and guess the 4 digit number in the least number of tries ')
print ('------------------------------------------------------------- ')
while tryagain == 'yes':
    lowoutput = None
    highoutput = None
    guess = None
    guess = input('Guess: ')
    if guess == number:
        print ('Correct! ')
        correctoutput str(raw_input(Enter 'exit' to exit: "))
        If correctoutput == 'exit':
            print 'Goodbye'
            exit()
        else:
            print 'invalid input'
    elif guess > number:
        print 'Your answer is too high'
        highoutput = str(raw_input("Try again? 'Y'/'N'))
        if highoutput == 'n':
            tryagain = 'n'
        elif highoutput == 'y'
            try again = 'yes'
        else:
            print 'invalid input'
    else:
        print 'Your answer is too low' 
        lowoutput = str(raw_input("Try again 'y'/'n'"))
        if lowoutput == 'n':
            try again = 'n'
        elif lowoutput == 'y'
            tryagain = 'yes'
        else:
            print 'invalid input'
print 'too bad, the right answer was: '
print (number)
exit()

This should work. There might be some mistakes cos I wrote this on my tablet. All improvements would be accepted

Tech Heck
  • 33
  • 1
  • 6

1 Answers1

5

try with

import random
r = random.randint(1111,9999)
print "%04d" % r

or

print '{0:04d}'.format(r)
Pynchia
  • 10,996
  • 5
  • 34
  • 43