This Piglatin programm does not seem to work


#1

Hi there, This Piglatin programm does not seem to work.
Does anyone here know what is wrong with my code?

pyg = 'ay'
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
  print original
  word = original.lower()
  #2
  first = word[0]
  #3
  new_word = word + first + pyg
  #4
  new_word = new_word [1:len(new_word)]
else:
  print 'empty'

Pig latin
#2

Hi there @betajumper23742 :smile:

I do know what is wrong. Let me ask you this question: What prints to the screen after you manipulate the string data? (This question is to make you think, not to help troubleshoot. Looking at your code I know the answer.)

For instance, if I type “Hello” i would bet that it prints “Hello” to the screen. Can you tell me why?


#3

Hi @x3rv , It prints out: Enter a Word:

And after that i try to enter a some raw input but it does not let me enter anything.


#4

Try refreshing the page. The syntax of your raw_input is correct. However, you will run into a logic error in the code.

You should be able to type something…


#5

Yes! now i can type again!

I typed hello and it prints it on the screen because i wrote print original
and original = raw_input

right?


#6

Exactly. Now, I think you got a good idea on how to change it to make it work as desired. Correct?


#7

Should i erase print ?


#8

Remember the goal is:
If raw_input is “Hello”

You want “ellohay” to print. That is suppose to be the result of the manipulation of your data.

While your answer to delete that line is correct, there is more to it. In order for the result to print, how might you change the code?


#9

That is true, I have changed my code to:

pyg = 'ay'
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
  print new_word
  word = original.lower()
  #2
  first = word[0]
  #3
  new_word = word + first + pyg
  #4
  new_word = new_word [1:len(new_word)]
else:
  print 'empty'

It works when i write “Hello”


#10

At the time of your print statement, what do you think the value of new_word might be?

Remember, you are to display the results of the data manipulation and code is executed from top to bottom unless instructed otherwise.


#11

I am sorry but i don’t quite understand what you are trying to explain.


#12

The only thing your program does not print is new_word, the expected output.

Sorry, @x3rv, it was getting drawn out.


#13

It’s a matter of when you are trying to print new_word.

I was trying to get you to think about what the value is vs what it should be :wink: and why.


#14

Hi, the program does print out the “new_word” when i click Run. Or are you reffering to something else?


#15

Your program should print it.

print new_word

#16

Do i have to move “print” to another line? or does that not really matter?


#17

Yes that is correct the program prints new_word.


#18

This logic is incorrect because you print the new_word before the manipulation instead of after the manipulation.


#19

In Python, statements on the same physical line need to be separated by semi-colons the same way as many other C-like languages, including Java and JavaScript.

Python lets us relax this when we write statements on their own physical lines.

orginal = raw_input("Enter a word to translate: ")
if len(original) > 0 and original.isalpha():
    print original
else:
    print "empty"

#20

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