startuprob
startuprob

Reputation: 1915

Simple Python concatenation question

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?

Upvotes: 0

Views: 123

Answers (2)

Sandro Munda
Sandro Munda

Reputation: 41030

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.

Upvotes: 1

Blender
Blender

Reputation: 298206

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.

Upvotes: 0

Related Questions