Python


#1

Hello,

I am having a very hard time with this section of loops. The instructions are; Use a while loop to let the user keep guessing so long as guesses_left is greater than zero.
Ask the user for their guess, just like the second example above.
If they guess correctly, print ‘You win!’ and break.
Decrement guesses_left by one.
Use an else: case after your while loop to print You lose.

And I am not sure what they are saying.

This is my code:

while guesses_left > 0:
    guess = int(raw_input("Your guess: "))
    print guess
    if guess == random_number: 
        print "You win!"
        break
    guess_left -= 1
    else:
        print "You lose."

But it didn’t works. So if everyone can help, please help me.


#2

each iteration of the loop you inform the user they lost, why?

you only want to inform the user they lost after they ran out of guesses (loop finished)


#3

Okay so much can I do that.


#4

i hope you can do this, yes :slight_smile:


#5

I don’t get what you mean.


#6

well, in python, what determines if something is nested inside or outside a loop?


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.