-5

I am completely new to python so please excuse me. I try to learn python on a website called Codecademy. It all works fine, however I wanted to see what would happen if I insert a script from the website into python on my PC. I have currently installed the second version of python (2.7.13).

The script is the following:

pyg = 'ay'
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = word [0]
    new_word = word + first + pyg
    # All the different variables are now in one
    new_word = new_word[1:len(new_word)]
    # The first two letters are removed
    print new_word
else:
    print 'empty'
    # If there is no input or any input containing non-letter characters

When I open this script via python, I am able to enter the first question, but as soon as I hit enter, the program closes and I can not get to my second question. I tried loading it via cmd directly, but it didn´t work either.

Arnaud
  • 7,259
  • 10
  • 50
  • 71
  • 1
    Possible duplicate of [How to keep a Python script output window open?](http://stackoverflow.com/questions/1000900/how-to-keep-a-python-script-output-window-open) – Josh Lee Mar 20 '17 at 18:44
  • 3
    What do you mean by "second question"? I assume by "first question" you mean the "Enter a word" prompt, but that's the only time you call `raw_input`. If you're thinking "I call `raw_input` a second time but I didn't think that part of the code was relevant to the problem", I do think it's relevant to the problem, so please share the entire code. – Kevin Mar 20 '17 at 18:45
  • That is because, you get only one raw_input from stdin. – Keerthana Prabhakaran Mar 20 '17 at 18:45

3 Answers3

1

Just add this line after your output:

raw_input('Enter any key to exit: ")

This will keep it open until you hit a key and click enter.

Jaff
  • 155
  • 1
  • 4
  • 14
0

There is no "second question" -- you have only one input statement in your control flow. If you want the program to repeat, you need to code that. A simple version:

while True:
    # Get user input
    original = raw_input('Enter a word:')
    # Convert to Pig Latin
    ... continue with your original code
    # Print result

Granted, this will loop forever. You can set a check in the loop for a "quit" input, if you like. I leave that as a further exercise for the student.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Sorry. I just noticed that I asked the wrong questions. My problem is, that once the program asks me to enter a word. ("Enter a word") Once I enter it, the program closes, without showing me the tranformed word. Sorry. Thank you! –  Mar 20 '17 at 20:20
  • In that case, your issue is a duplicate of the question Josh Lee referenced in the very first comment. – Prune Mar 20 '17 at 20:40
0

The complete solution of your query: Note(You said the first two letter is removed but in your code you wrote [1:]-- that means it removed only first letter)

pyg = 'ay'
want_continue = True
while want_continue:
    original = raw_input('Enter a word:')
    if len(original) > 0 and original.isalpha():
        word = original.lower()
        first = word [0]
        new_word = word + first + pyg
        # All the different variables are now in one
        new_word = new_word[2:len(new_word)]
        # The first two letters are removed
        print new_word
    else:
        print 'empty'
        # If there is no input or any input containing non-letter characters 
    user_input = raw_input('Want to Contiue:(Y)')
    if user_input.lower() != 'y':
        want_continue = False
Dario
  • 6,152
  • 9
  • 39
  • 50
PramodG
  • 40
  • 1