8. Your own while/else (python - the loops- 8/19)


#1

Hello,
This time I try to do the codes in English ... not easy because I have to translate constantly.

I always hangs on the 8 !!!

The validation is not for exo following and it said:
"Oops, thank you again. Do not forget to increment counts!"

Here is my code:

from random import randint


random_number = randint(1, 10)

guesses_left = 3

while guesses_left > 0:
    guess = int(raw_input("Your guess for 1 and 9 :"))
    
    if guess == 3:
        print "You win!"
        break;
        
    guesses_left -= 1

else:
    print "You lose."

In past 72hrs this code I did not find something wrong!

I try to post on the forum in French but it did give anything ...
I look for answers on the forums but I did not find anything !!!

Frankly it embêterais me have to stop there way ... and then I do not think there was a better site than this one to learn.

Thank you for your help !


#2

Hi I just ran your code and it works


#3

I do not understand why it gives me this error message


#4

But the error you get says

"Oops, thank you again. Do not forget to increment counts!"

Aske you to increment counts maybe its another lesson?


#5

the other lesson? What other lesson? I can not pass that has the ...
an old lesson ??
:confused:


#6

Hi can you take a screenshot of your code ?


#7

Yes but I do not know how to put it on the forum

:confused:


#8

Here click one that one and then choose your
Image on your computer.


#9

thank you I know how to do now ... as you see it's the same code ...


#10

I just realized that if I change language option, going from English to French, I have to start all over again ...


#11

Hi can you take the screenshot with the instructions, and your code?


#12

@wizmarco You see the catch ??? the instructions are in French ...


#13

@wizmarco has arrived you see screenshot ... I posted 2 times ...


#14

I realy dont see where the error is


#15

Me either ...
I think I'll have to start over going to the english version :frowning: :confused:


#16

Hello.
May I know why the else statement has been indented to the left. I am not able to understand this concept.
Under the while loop comes the if and else. But looking at the code, I see only the 'If' statement coming under the while loop and not the else statement. Could you please explain this to me. Thanks :slight_smile:


#17

HI the indent in Javascript isn't important like in Python in Javascipt you could write your code without indent like that

if (10 === 5 * 2) {
console.log("hello")
}
else {
console.log("Hi")
}

Is the same as

if (10 === 5 * 2) {
    console.log("hello")
}
else {
    console.log("Hi")
}

even inside function

var test = function() {
if (10 === 5 * 2) {
console.log("hello")
}
else {
    console.log("Hi")
}

}

is the same as

var test = function() {
    if (10 === 5 * 2) {
       console.log("hello")
    }
    else {
        console.log("Hi")
    }

}

But in Python you should always care about indent

if 10 == 5 * 2:
print "Helo"
else
print "Hi"

thi will says you...

IndentationError: expected an indented block

so it shoudl be

if 10 == 5 * 2:
    print "Helo" #four space before typing print
else
    print "Hi"#four space before typing print

inside the function in Python you should also indent everything inisde the function

def test():
    if 10 == 5 * 2:
        print "Hello"
    else:
        print "Hi"

Hope that could help you.


#18

hi, you've done it correctly but in line 7 it must be "if guess == random_number:" instead of " if guess == 3:"
from random import randint

random_number = randint(1, 10)

guesses_left = 3

while guesses_left > 0:
guess = int(raw_input("Your guess for 1 and 9 :"))

if guess == random_number:
    print "You win!"
    break;

guesses_left -= 1

else:
print "You lose."


#19

Hello
why did you put
if guess == 3 :
print"you win"
i think
it's not the goal of the game !
the goal is to check if the user is making a right guess to accept it so the code will be
if random_number == guess :
print "You Win"
please correct me if i'm wrong


#20

Someone can help me with my code? Here is what i have:

from random import randint

guesses_left = 3
guess = int(raw_input("Guess the number or Try again:"))

  while guesses_left > 0:
      random_number = randint(1, 10)
      if guess != random_number:
    
      print "Try again you left: %r tries" % guesses_left
  break
        
  else guess == random_number:
  	print "Congratulations! You have guessed" guess

guesses_left -= 1