0

If I type the following into IDLE, it gives me the result of 'wordwordword':

print("word" * 3)

If I go through the following steps in IDLE, it gives me the same result:

sentence = input() #I type "word"
number = int(input()) #I type it to int because input() saves as a string, I type "3"
print(sentence * number)

But then, if I try to use the exact same three lines above in a Notepad document to create it as a script, I only get the result of 'word' instead of 'wordwordword'

Any thoughts?

7
  • 1
    Without the actual output and the Python version number, we'll have a hard time guessing. Commented Apr 25, 2011 at 17:23
  • Cannot reproduce. It prints "wordwordword" in my pc when run as script. Maybe you overlooked something in your saved script? (I'm running Python 2.7, since you're using print function and input, I assume you're using Python 3.x) Commented Apr 25, 2011 at 17:24
  • Works for me too. Version info would be useful. Commented Apr 25, 2011 at 17:25
  • I am using the 3.x Python version. I figured that since I am new to the language, I should try to learn the newest version - is that faulty logic? Commented Apr 25, 2011 at 17:26
  • Yes, you should learn on 2.7. Most of the tutorials and libraries are written for 2.7 Commented Apr 25, 2011 at 17:29

2 Answers 2

1

You code works well in python 3.

With python 2, just replace input by raw_input() like that :

sentence = raw_input()
number = int(raw_input())
print(sentence * number)

You can read the PEP 3111 to understand the difference and the motivation between input and raw_input in python2 and python3.

Sign up to request clarification or add additional context in comments.

5 Comments

@Rob: Did you accept this answer even though your code doesn't work?
There is no problem with my code, it works well with python2 ;-) The code of @Rob works well in python3.
@Sandro Munda: Rob's comment seems to indicate it is not working.
@S.Lott Try it. It's not a problem due to my code. I think my answer was accepted because Rob has realized.If there is a problem, I'm listening.
@Sandro Munda: I'm still not commenting on your code or your explanation. I'm still asking about Rob's comment, which seems to indicate it is not working.
0

I'd try raw_input():

sentence = raw_input()
number = int(raw_input())
print(sentence * number)

The documentation says that:

input([prompt])

Equivalent to eval(raw_input(prompt)).

eval() executes Python code, which isn't what you want.

2 Comments

This throws a syntax error. I am using the newest version of Python, could that be a problem? Even when I just use a simple 'raw_input()' I get an error.
Ohhhh, never mind. Python3 renamed raw_input() to input(), which now makes sense. Carry on ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.