Simple errors


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"
<img src="//" width="690" height="305">


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


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?


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


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.


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


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


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


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