python - How to create a random 4 digit number and store it as a variable -
this question has answer here:
i working on guess-the-4-digit-number game in python 2.7.10. cannot find how randomly make 4 digit number, , store variable. digit has somewhere between 1111 , 9999 hard part is, want store variable, , not print out player see.
if want to, i'll post code when i'm finished.
the code here!
import random number = random.randint(1111,9999) tryagain = 'yes' print ('hello! ') print ('------------------------------------------------------------- ') print ('try , guess 4 digit number in 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' exit: ")) if correctoutput == 'exit': print 'goodbye' exit() else: print 'invalid input' elif guess > number: print 'your answer 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 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, right answer was: ' print (number) exit()
this should work. there might mistakes cos wrote on tablet. improvements accepted
try with
import random r = random.randint(1111,9999) print "%04d" % r
or
print '{0:04d}'.format(r)
Comments
Post a Comment