I had it solved: Your own while / else


#1

I had the bloody thing solved...but not the way CodeAcademy wanted. I wasted 45 minutes trying to figure out how CA wanted the answer...frustrating to say the least. There should be something that acknowledges a correct answer and indicates that there is another way. To stop the progress of a programmer is idiotic.


#2

Hi @danathurston ,

Could we take a look at the code that you submitted for this exercise, but that Codecademy did not accept? We'll try to figure out why Codecademy did not like it.

Here's a link to the exercise: Your own while / else.

Please remember to format the code when you post it. For advice on how to do that, see How do I format code in my posts?.


#3

No, I fixed it so CA would pass me. I am having the same problem in the next bit. My code works but not your way.


#4

@danathurston ,

Then you're welcome to post your code, formatted, for the next bit, if you want us to figure out why Codecademy does not like it.


#5

Thanks for responding, Glenn. I have programmed in a ton of languages and
many times, the instructions offered can be confusing or misleading for an
experienced programmer. I just spent 15 minutes on the replacing char with
"X" until I gave up and looked at the hint: you cannot use "if char == "A"
or "a":" It has to be char == "A" or char == "a". It is little things like
this that are so frustrating.

I would dare say if I had to write a training course in assembler, I might
find the same types of problems.

Dana


#6

HI, Dana.

Thanks for the example code.

In Python, the or operator performs a "short-cut" evaluation of expressions. If the operand on the left side of the or does not evaluate to something that corresponds to False, then the operation immediately evaluates to the value of the left operand without evaluating the right operand at all.

Also, or has precedence over == concerning order of operations.

Since "A" is not a False value, "A" or "a" evaluates to "A". Therefore, ...

if char == "A" or "a":

... only executes its block of code if char == "A" is True.


#7

I have right now the same problem, my code looks this way
while guesses_left>0:
guess = int(raw_input("Choose your number:"))
if guess==random_number:
print "You Win!"
break
else:
guesses_left -= 1
if guesses_left==0:
print "You Lose!"

but I'm getting this error:
Oops, try again. Did you include an else for the while loop?

BTW. I've already done it


#8

Exactly, Acrodan...it took me a while to see that they wanted the "else" in
the loop. But a loop is just that, a loop. Loop until a certain condition
is met. The "else" in there made no sense to me.


#9

And I get it now.

while number-of-purchases > 0


#10

while number-of-purchases > 0
print my-purchases
else
print "You have made no purchases"

There is a method to the madness but a new programmer coming into the
course may not even consider this reasoning.


#11

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