Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
All is working but one
#1


I'm a beginning coder learning from Codecademy. I'm using Python 2(because that's what Codecademy teaches). I'm building a pyg latin translator based off of the lesson on Codecademy except I'm making it so you can translate phrases rather than a single word. The problem I'm having is with my code that makes it so you can leave a blank when it asks to do one word, next word, etc. All of them work except for the fifth word, and what it does is it allows you to leave it blank but it doesn't allow you to input a word. Here is my code:

pyg = "ay"
original = raw_input("Please enter a word:")
second = raw_input("Please enter a second word:")
third = raw_input("Please enter a third word:")
fourth = raw_input("Please enter a fourth word:")
fifth = raw_input("Please enter a fifth word:")
if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
new_word = word + first + pyg
new_word = new_word[1:len(new_word)]
if len(second) > 0 and second.isalpha():
word_two = second.lower()
second_two = word_two[0]
new_word_two = word_two + second_two + pyg
new_word_two = new_word_two[1:len(new_word_two)]
if len(third) > 0 and third.isalpha():
word_three = third.lower()
third_two = word_three[0]
new_word_three = word_three + third_two + pyg
new_word_three = new_word_three[1:len(new_word_three)]
if len(fourth) > 0 and fourth.isalpha():
word_four = fourth.lower()
fourth_two = word_four[0]
new_word_four = word_four + fourth_two + pyg
new_word_four = new_word_four[1:len(new_word_four)]
if len(fifth) > 0 and fifth.isalpha():
word_five = fifth.lower()
fifth_two = word_five[0]
new_word_five = word_five + fifth_two + pyg
new_word_five = new_word_five[1:len(new_word_five)]
if len(original) == 0:
new_word = original
if len(second) == 0:
new_word_two = second
if len(third) == 0:
new_word_three = third
if len(fourth) == 0:
new_word_four = fourth
if len(fifth) == 0:
new_word_five = fifth

So the problem is with the very last bit of code, the "if len(fifth) == 0"... the first four work just fine, but the last one seems to be cancelling out the new_word_five variable up where it shows "new_word_five[1:len(new_word_five)]. Please help... I'm a very noob beginner and this is my first program I'm doing by myself.
Reply
#2
Do you have a link for Codecademy Python course ?
Reply
#3
Are you sure you should be using len(fifth) == 0?

To rephrase that, in English, you're saying that...
if thing_named_fifth is empty and has nothing in it:
    thing_named_new_word_five should also be empty and have nothing in it.
Reply
#4
Quote:So the problem is with the very last bit of code, the "if len(fifth) == 0"... the first four work just fine, but the last one seems to be cancelling out the new_word_five variable up where it shows "new_word_five[1:len(new_word_five)]. Please help... I'm a very noob beginner and this is my first program I'm doing by myself.

nilamo has probably a better explanation than me.

if len(fifth) == 0:
  new_word_five = fifth
Problem is with what your doing with the len(fifth). len is a function which returns the total length of the fifth string which you just read from stadin (user). So you wan assign the the string in fifth to new_word_five, only if the user entered anything at all.

Len function does help to tell, if the length of the string is larger than 0. What you need here is

# only assign when the length is greater than 0
if len(fifth) > 0:
   ...

OR just and make len redundant

if fifth:
   ...
does that make sense?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020