开发者

Simple Python concatenation question

开发者 https://www.devze.com 2023-02-28 09:15 出处:网络
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:

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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消