mardi 16 janvier 2018

concluding statement trouble:number guessing game python

The following code is for a simple number guessing game in python that I made. The first version works flawlessly.But the second one,in which I have only modified the closing if statement causes the code following the statement to not run.Help!

PS:I'm a newbie with one day of programming experience.Pls. explain accordingly.

Valid Code

    from random import randint
    tries = 0
    secret = randint(1,10)
    for tries in range(5):
        guess=int(input("Try to guess the number between 1 and 10 that 
        I'm thinking of."))
        tries+=1
        if guess<secret:
            print("Guess higher!")
        elif guess>secret:
            print("Guess Lower!")
        else:
            print("Whoa!You got it right!The number I was thinking of 
            was %s and you guessed it in %s tries."%(secret,tries))
            break
    if guess!=secret:
        print("Sorry,you\'re out of tries.The number was %s"%secret)

Wrong code

     from random import randint
    tries = 0
    secret = randint(1,10)
    for tries in range(5):
        guess=int(input("Try to guess the number between 1 and 10 that 
        I'm thinking of."))
        tries+=1
        if guess<secret:
            print("Guess higher!")
        elif guess>secret:
            print("Guess Lower!")
        else:
            print("Whoa!You got it right!The number I was thinking of 
            was %s and you guessed it in %s tries."%(secret,tries))
            break
    if tries>5:
        print("Sorry,you\'re out of tries.The number was %s"%secret)

Aucun commentaire:

Enregistrer un commentaire