Can anybody help me with what's wrong with this code?


#1

Hello,

I just started coding at code academy and it’s a blast. I’m having fun and learning with the PygLatin translator. But as of right now, there is something I don’t seem to understand with this particular piece of coding I have presented here.

At the first conditional statement, the ‘if’, there seems to be a problem. Without a boolean, the code seems to work fine, but when the boolean ‘or’ is introduced, the code always seems to put out the message ‘dude, be polite’.

Maybe it’s something really easy I just looked over, but it would be great if I understand the mistake.

Any help is appericiated!

yay = 'ay'

original = raw_input('Hey there, please type in a word and press enter: ')

if original == 'boring' or 'no':
  print 'dude, be polite'
elif len(original) > 0 and original.isalpha():
  word = original.lower()
  first = word[0]
  new_word = word + first + yay
  new_word = new_word[1:len(new_word)]
  
  print new_word
else: print 'please try again'


#2

First, remove all the embellishments and try again. Check the instructions and be sure you have followed them to the letter.


#3

I have completed the assigments already, So I started to play around with it for learning purposes. Do you Maybe see What is wrong with the code?


#4

Is all the input resulting in,

dude, be polite

?

Start with figuring that problem out.


#5

This is read as:

(original == 'boring') or 'no'

not

original == ('boring or 'no')
Which by the way is equivalent to:
original == 'boring'

The lesson here being that nothing does what you want because you want it. You’d consider the exact behaviour of the thing you’re using, and leverage that, ensuring things are lined up to get the effect you’re after.

For the most part, you ought to be using and/or to combine two truths. Not text.


#6

yes, all the input results in,

dude, be polite

I figured that the problem must lie with the or operator, because translator works properly when the code only says,

if original == 'boring'

But I don’t seem to understand how this turns everything you type in the console results in the ‘dude, be polite’ string.


#7

Allright your explanation clears things up a bit, but what would be the correct thing to do to make the code work the way I want it to?

I want it to output the string ‘dude, be polite’, when either the word ‘boring’ or ‘no’ is entered, and if not the code should procede to the elif statement.

Could you help me with this?


#8

I’ll expand on @ionatan 's answer a little. They explained the error in your code:

For the most part, you ought to be using and/or to combine two truths. Not text.

Conditionals, like if, work out whether one or more statements are true or false and then run code based on that decision.

If I were to provide you with a generic example:

if test1 or test2:
  my_function(a)
elif test3:
  my_function(b)
else:
  print "Huh?"

and that, in your code, you’ve already covered “test1” as:

original == "boring"

can you work out what you need to change to get your code working as you’d like?

(I’m trying to nudge you towards the fix without telling you directly what it is.)

( Edit: I made an assumption, which I have corrected. I also made a formatting error… Oops. )


#9

How does OR work?

if A or B: # do something
else: # do something else

What are A and B representative of? Answer: Expressions.

What is an expression? Answer: Any form of statement (see note) that returns a value. In the case of OR, the values (operands) are evaluated as booleans.

if A: #do something
elif B: # do something else
else: # default action

Now consider,

A = "boring"
B = "no"

print (A or False)    # boring
print (B or False)    # no

That means both A and B evaluate to True. See the light, yet?

A non-empty string will ALWAYS evaluate to True. In the following,

if A == "boring" or B:

Will always return B when A is False, since B is always True.

Note:

statement, with respect to a phrase or declaration; not to be confusted with program statement.

The following are all expressions as we would encounter them…

""
''
"yes"
'no'

Above are string literal expressions.

a == b
a < b
b > b
a >= b
a <= b
a != b

Above are comparison expressions.

We could go on, but one will begin to see the picture.


More notes:

Functional representation of OR

>>> def aORb(a, b):
    if a:
        return True
    elif b:
        return True
    else:
        return False

>>> print (aORb('',''))
False
>>> print (aORb('boring','no'))
True
>>> print (aORb('', 'no'))
True
>>> a = 'boring'
>>> print (aORb(a == 'boring', 'no'))
True
>>> a = ''
>>> print (aORb(a == 'boring', 'no'))
True
>>> 

Technincally, OR will return the operand itself…

>>> def aORb(a, b):
    if a:
        return a
    elif b:
        return b
    else:
        return b

>>> 

#10

or evaluates to the left side if the left side is truthy, otherwise to the right side

1 or 2  # 1
0 or 3  # 3
'boring' or 'no'  # 'boring'
[] or 5  # 5
'' or 'aoeu'  # 'aoeu'
'aoeu' or ''  # 'aoeu'
1 or print('this does not get printed')  # 1
print('this does get printed') or 1  # 1

Additionally, the right side is only evaluated if the left side is falsy

And, again, the two inputs are in most cases booleans. Not numbers, strings, lists etc. (But that does have well-defined behaviour)

Whether a value is truthy or not is something the value decides itself. This is also what determines conversion to bool. Empty or lack-of-value is typically falsy, and the rest truthy.

Similarly, and evaluates to the left side if it is falsy, and otherwise to the right side. It too is short-circuited, it only evaluates the right side when the left side is truthy.

They mean “at least one” or “both” respectively.


#11

This definetly cleared everything up. Because of these explanations I won’t be making a mistake like this again. I changed the code to what’s written below, and it seemed to fix the problem.

if original == 'boring' or original == 'no':
  print 'dude, be polite'

The code is working as I had intended now,
Thank you for taking your time to deliver such a thorough explanation.


#12

I’ve now got a better understanding of the subject, your replies provided a good source of knowledge. I changed the code to the following and it worked.

if original == 'boring' or original == 'no':
  print 'dude, be polite'

Thank you for taking your time to write up these replies!


#14

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