Need help with question 7 on introduction python

python
non-track-course

#1

Course: https://www.codecademy.com/en/courses/python-beginner-en-kSQwt/0/7

hi I have just started learning python and came to question on introduction python it asked me to use the if for the question some how it didn't do was I ask. I don't know where I got it wrong. Please help!!

        Exercise goal: Practice making choice using if.

We are composing some Tweets for our Twitter followers, but are stuck
on the fact that Twitter messages can't be more than 140 characters! We
need to only print our messages if they are short enough for Twitter.

So far, we have 3 variables: message1, message2, and message3.

      Your job:

For each of these messages, if the message is less than or equal to 140 characters, print the message; if it is longer than 140 characters, don't print it.

Add your code below the existing code, starting on line 4.

(https://www.codecademy.com/en/courses/python-beginner-en-kSQwt/0/7)

message1 = "The Analytical Engine weaves algebraic patterns, just as the Jacquard loom weaves flowers and leaves. -- Ada Lovelace, the first programmer"
message2 = "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal."
message3 = "They told me computers could only do arithmetic. -- Computer pioneer Grace Hopper"
if (len(message1) <= "140"):
print(message1)
elif (len(message2) <="140"):
print(message2)
elif (len(message3) <="140"):
print(message3)
else:
print()

the error message said:

Oops, try again.

I didn't see:

They told me computers could only do arithmetic. -- Computer pioneer Grace Hopper

when I ran your code, but it is less than 140 characters!
Double-check your `if` logic and try again.

Please help me out! Thank you!


#2

Hello Cjwliu :slight_smile:

You are going in the correct direction. The only place where you are going wrong is when you use elif.

By using elif, you are effectively stating that if message1 gets printed, then no other message should get printed, even though they might be below 140 characters! How, you ask? Well, that's because an elif branch only executes if the if branch fails. If the if branch is true, then neither elif nor the else branch execute.

You need to independently check if each message has its length less than 140 characters.

Can you now fix the problem? :wink:


#3

I am still not sure how to fix it.


#4

if (len(message1) <= "140"):
    print(message1)
else:
    print(message2)
if (len(message3) <="140"):
    print(message3)
else:
    print()

does that work?


#5

Well, not quite. Re-read this:

By independently check, I mean to say that whether or not you print message2 should NOT depend on the outcome of message1.

When you use else or elif,

if message1 condition:
    # do stuff
elif message2 condition:
    # do stuff

you are stating that: If message1 condition was true, do not check for message2. Thus, you are making the check for mesage2 dependent on message1.

I will take the hint a little further, you need to use independent if statements (no elif or else) to check each message.


#6

if (len(message1) <= "140"or len(message2)<= "140"):
    print(message1 or message2)
else:
    print()
if (len(message3) <="140"):
    print(message3)
else:
    print()

am I getting it right?


#7

You made some progress! :tada: But not quite correct yet. Let me finally reveal the code structure to you:

if condition for message1:
    # do stuff

if condition for message2:
    # do stuff

if condition for message3:
    # do stuff

Does that make sense? Can you relate it to my previous hint?


#8

I tried to use just with the if statement, it gave me messages asked me to check my if statement again

if (len(message1) <= "140"):
print(message1)
if (len(message2)<= "140"):
print(message2)
if (len(message3) <= "140"):
print(message3)

it shows error message:

Oops, try again.

You printed:

The Analytical Engine weaves algebraic patterns, just as the Jacquard

loom weaves flowers and leaves. -- Ada Lovelace, the first programmer
Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in liberty, and dedicated to the
proposition that all men are created equal.
They told me computers could only do arithmetic. -- Computer pioneer
Grace Hopper

But I think that's longer than 140 characters.
Double-check your `if` logic.

don't know where I got it wrong again. sorry for the trouble.


#9

Oh! Note that the numbers in your if condition are supposed to be integers like 140 and not strings like "140".


#10

oh I see now thank you every much for your help!


#11

Hi I got stuck on question 14. would love some help please!

      
        ZZZ
      
      
        Exercise goal: Practice using lists and for loops.

You need some Z words for your Scrabble game!

words is a list of words.

      
    
    
      
        Instructions
      
      
        Your job:

Use a for loop to print out only the words that start or end with the letter "z".

I wrote:

words = ["pizazz", "python", "zebra", "pizza"]
for word in words:
    if word[0:-1] in "z":
        print(word)

the programme ran and said: Oops, try again.I expected to see pizazz but didn't!

Please give me some pointer where I had it wrong? thank you!


#12

Hint: extracting last character: word[-1:], first character: word[0], so simple condition:

if "z" in toLowerCase(first + last character)

or

if "z" == toLowerCase(first character) or "z" == toLowerCase(last character)

#14

Hi, I been stuck for a while for with this exercise but i finally came up with this code which give me lots of errors, the funny thing is, i was allow to move the next stage of the exercise.
Your job: Use a for loop to print out only the words that start or end with the letter β€œz”.

words = [β€œpizazz”, β€œpython”, β€œzebra”, β€œpizza”]
word = words
for word[0:]in words:
print(word)
Unknown Error: , β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜i’, β€˜p’, β€˜i’, β€˜z’, β€˜a’, β€˜z’, β€˜z’, β€˜pizazz’, β€˜python’, β€˜zebra’, β€˜pizza’] this is the error i got but i’m allow to move on which is strange. I want to move on the right so can someone help me with the rightful code?


#17