python - Modfying current code -


my next task modifying current code. in previous exercise, i've written basic application covers numbers guessing game. code follows: -

# guess number # # computer picks random number between 1 , 100 # player tries guess , computer lets # player know if guess high, low # or right on money  import random    print("\twelcome 'guess number'!") print("\ni'm thinking of number between 1 , 100.") print("try guess in few attempts possible.\n")  # set initial values the_number = random.randint(1, 100) guess = int(input("take guess: ")) tries = 1  # guessing loop while guess != the_number:     if guess > the_number:         print("lower...")     else:         print("higher...")      guess = int(input("take guess: "))     tries += 1  print("you guessed it!  number was", the_number) print("and took you", tries, "tries!\n")  input("\n\npress enter key exit.") 

my task modify there limited number of goes before failure message given user. far, chapter has covered "if, elif, else, for, loops, avoiding infinte loops." such, i'd limit response these concepts only. loops covered next chapter.

what have tried?

so far, i've tried amending block in while loop using 5 goes , tries variable doesn't seem work.

# guessing loop while tries < 6:     guess = int(input("take guess: "))     if guess > the_number:         print("lower...")     elif guess < the_number:         print("higher...")     elif guess == the_number:         print("you guessed it!  number was", the_number)         print("and took you", tries, "tries!\n")     break     tries += 1  input("you didn't in time!") input("\n\npress enter key exit.") 

any pointers or highlighting i've missed appreciated plus explanation i'd missed. teaching myself think programatically aslo proving tricky.

what doesn't work when run it, loop conditions't don't appear work. idle feedback follows.

this means question can summarised looping logic broken?

>>> ================================ restart ================================ >>>      welcome 'guess number'!  i'm thinking of number between 1 , 100. try guess in few attempts possible.  take guess: 2 take guess: 5 higher... didn't in time!   press enter key exit. 

the problem break statement not indented included in elif:

elif guess == the_number:     print("you guessed it!  number was", the_number)     print("and took you", tries, "tries!\n") break 

thus, loop stops after first iteration. indent break included within elif , should work.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -