Simple errors


#1



https://www.codecademy.com/en/courses/python-beginner-en-cxMGf/0/4?curriculum_id=4f89dab3d788890003000096#


So this code works for a pass in the exercise; my only problem with it is for a "y" input it does not send the user a print response for which I had programmed it to do so.


I expected a response for a "y" input to be "keep it up." Although the print response for a "n" does invoke a print response to the user "try harder". How can I get my code to give a user a response for a "y" input from the user?


Replace this line with your code. 
choice = raw_input('Enjoying the course? (y/n)')

while choice != "y" and choice != "n":  # Fill in the condition (before the colon)
    choice = raw_input("Sorry, I didn't catch that. Enter again: ")
    if choice == "y":
        print "keep it up"
    if choice == "n":
        print "try harder"
        break
<img src="//codecademy-discourse.s3.amazonaws.com/original/5X/8/3/4/6/83467a3f55b27e3beebf6ef130d769cc8b3235e8.png" width="690" height="305">


#2

if you enter y:

Enjoying the course? (y/n) y

then the loop condition evaluates to false, so the loop never runs. So the conditions/print statements are never reached


#3

I don't quite understand, why it would evaluate to false. From what I understand so far the comparison operator when used should return true for the user inputting the correct declaration for the comparison. Since I set the the variable choice to identify the string "y" as being true for the input performed by the user. Any other value the user is prompted to input a recognizable value identifiable by the program. Okay so the program recognizes the string value as false then how can I make it true?


#4

if the user enters something invalid (for example "x" you get:

Enjoying the course? (y/n) x
while "x" != "y" and "x" != "n"

x does not equal y, true.
x does not equal n, true.

so the loop will make an iteration. if the user enters "y" you get:

Enjoying the course? (y/n) y
while "y" != "y" and "x" != "y"

y does not equal y, false
y does not equal n, true

false and true is false

the easiest solution would be to move the if conditions outside the loop. This way they are always reached after you ensured you have valid input.

Personally, i wouldn't use two if's here, i would use if and elif


#5

So it isn't until I input an incorrect character that the code recognizes a true value to print out the the statement that correlates to the recognizable string value. So how do I get the program to recognize the string value on the first try because the first try the print statement is not printed. Basically going back to what you were saying earlier. I thought maybe a return instead of a print would have solved the issue but I cant get the indentation right to be able to put the return invocation in.


#6

i covered this? When placing the if clauses outside the loop, they are even reached for valid first input.


#7

When you say "inside" and "outside" the loop in terms of indentation what is considered "outside" what is considered "inside"?


#8

at this point in the course, i assumed this would be known? What is nested inside the loop has indent (4 spaces/tab is common) if you want to place something outside the loop, it should have no indent


#9

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