Trouble understanding 4/19


#1



I expected the 'or' function to be used here but instead it's 'and'. I tried to talk myself through and don't quite understand what is going on with the 'or' function here


choice = raw_input('Enjoying the course? (y/n)')
#as long as 'choice' is not equal to string 'y' or 'choice' is not equal to string 'n' the while loop continues no matter what is entered
while choice != 'y' or choice != 'n':  
    choice = raw_input("Sorry, I didn't catch that. Enter again: ")



choice = raw_input('Enjoying the course? (y/n)')
#as long as 'choice' is not equal to string 'y' and 'choice' is not equal to string 'n' the while loop continues
while choice != 'y' and choice != 'n':  '
    choice = raw_input("Sorry, I didn't catch that. Enter again: ")


#2

lets look at or first. In case of or, if one or both conditions are true, the condition evaluates to true. So, lets say the user enters something invalid, both conditions would be true (x doesn't equal n, which is true. x doesn't equal y, which is also true)

If the user enters something valid, lets say y. y doesn't equal n is true, y doesn't equal y is false. false or true evaluates to true, the loop will keep running (the while loop runs for as long as the condition is true)

Now lets look at and. If the user enters something invalid, both conditions are true. The loop will keep running. If the user enters something valid, for example y, y doesn't equal n is true, y equals y is false. true and false evaluates to false, the loop ends, as it should, since the user enters something valid.

Confusing isn't it?


#3

A bit, I guess thinking that it was a boolean statement didn't really click for me until you mentioned it, even though it's mentioned in the lesson. I was thinking it was a logical thing like "if (this) or (this) then: (this happens)"


#4

If it only where that simple, do you understand now? So far i know you could also simple use:

while choice == 'y' or choice == 'n':

but then again, using != is good to improve your understanding of Booleans and if/loop conditions


#5

Yes, it took me a couple minutes to process the whole thing, I think of programming like an epic form of math and thought that the reverse statement might work, but I didn't think so. I've noticed that my code usually has syntax errors and even though I haven't finished the courses on this site I'm already writing scripts and making small contributions looking at other code outside of this site and spotting mistakes for them.


#6

If a boolean expression is hard to read then you can probably re-phrase it.

Multiple not's is something to avoid:

choice != 'y' and choice != 'n'

Is equivalent to:

not (choice == 'y' or choice == 'n')

Expressions can be split up and named:

valid_choice = choice == 'y' or choice == 'n'
while not valid_choice:
    ...

And in Python we can also do:

choice not in 'yn'

#7

Thanks for the alternatives. Right now I'm wondering why this code throws an error in 8. I know that if int(guess) == random_number: causes an error message when compiled in the browser and if guess == random_number: allows me to pass but makes the games unable to be won because guess is a string

from random import randint
random_number = randint(1, 10)

guesses_left = 3
print random_number
while guesses_left > 0:
    guess = raw_input('Guess the number between 1 & 9: ')
    if int(guess) == random_number:
        print 'You Win!'
        break
    guesses_left -= 1
else:
    print 'You Lose.'

#8

The only thing that is checked during compilation is syntax.
Python doesn't do much in ways of compiling, mostly it's just the text into byte-code. Python is generally thought of as interpreted, not compiled. It doesn't run in your browser when you do exercises, it runs on some server elsewhere.

Read the error message. It probably says exactly what's wrong.


#9

"Did you allow the user 3 guesses, or did you incorrectly detect a correct guess?"

Nope, kind doesn't tell me whats wrong, I'm guessing it has something to do with the break, the code works great though


#10

You won but guess wasn't equal to random_number (different types)


#11

I can't win with guess == random_number but it lets me pass the lesson

I can win with int(guess) == random_number but it unfortunately throws an error that doesn't let me pass the lesson

So I need to figure out whats wrong with my code


#12

It expects guess to be equal to random_number when you win


#13

THANK YOU! I was just about to try guess = int(raw_input('Guess the number between 1 & 9: ')) too. I'm trying to help a friend with earlier lessons. It throws an error with that though which is why I was avoiding it