如何根据输入打印某些#次

时间:2016-11-22 15:28:20

标签: python

我不打算制作一个程序,根据另一个输入打印输入一定次数。

msg = input ("What is your message?")
times = input ("How many times do you wan't your message repeated?")
for times in range(6):
    print(msg)

2 个答案:

答案 0 :(得分:1)

如果要输入字符串,则必须使用raw_input()。您的范围运算符中也存在错误。像这样使用它:

msg = raw_input('What is your message?')
times = raw_input("How many times do you wan't your message repeated?")
for i in range(int(times)):
    print(msg)

输出:

What is your message? Hello World.
How many times do you wan't your message repeated?3
 Hello World.
 Hello World.
 Hello World.

Process finished with exit code 0

答案 1 :(得分:0)

您需要迭代0到times之间的一系列数字,但是您还需要将times转换为整数。

为此,for循环必须为for i in range(int(times)):