python - Code for a random secret number -


the code allows me more 6 times input , did not print else statement. code is:

import random secret = random.randint(1, 99) guess = 0 tries = 0 print ('ahoy! dread prites roberts , , have secret!') print ('it number 1 99. i\'ll give 6 tries ')  while guess != secret , tries < 6: guess = int(input('what guess? ')) if guess < secret:      print ('too low, scurvy dog!') elif guess > secret:      print ('too high, boy')      tries = tries + 1 elif guess == secret:       print ('avast! got ! found seceret , did!') else:      print ('no more guess! better luck next time')      print ('the secret number was',secret) 

i tried code in python 3.4. prints result more 6 times. while guess not equal secret , tries... after 6 tries print 'no more guess better luck next time' executing again , again

your have indentation problem (i guess happened pasting) main problem is, incrementing tries when guess high. should move last if else out of while block, since while condition taking care of vars.

your implementation should this:

import random secret = random.randint(1, 99) guess = 0 tries = 0 print ('ahoy! dread prites roberts , , have secret!') print ('it number 1 99. i\'ll give 6 tries ')  while guess != secret , tries < 6:     guess = int(input('what guess? '))     tries = tries + 1     if guess < secret:          print ('too low, scurvy dog!')     elif guess > secret:          print ('too high, boy')  if guess == secret:      print ('avast! got ! found seceret , did!') else:     print ('no more guess! better luck next time')     print ('the secret number was',secret) 

Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -