Python:用户输入数字,然后输入一个字母,然后按照数字所说的次数输出字母

Python: User enters number, then a letter, then outputs the letter the amount of times as the number says

Python: 用户输入数字,然后输入一个字母,然后按数字表示的次数输出字母:

例如

"Enter Integer": 4
"Enter Letter": a

输出

a
a
a
a

这是我目前拥有的,但我收到名称错误,“ ”未定义,“ ”是字母

integer = int(input("Enter a positive integer: "))

character = str(input("Enter a character, e.g. 'a': "))

for i in range(integer):
    print str(character)

如果我输入 4, 4 它会给我

4
4
4
4

可以,但是不会输出字母,我是新手 python 所以你得打扰一下

有什么想法吗?

Link 的错误:https://imgur.com/7pKMp3y

对python使用raw_input 2.7.

integer = raw_input("Enter a positive integer: ")

character = raw_input("Enter a character, e.g. 'a': ")

for i in range(int(integer)):
    print character

请参阅 this 堆栈溢出问题以了解有关输入与 raw_input 在 python 2.7 中的解释。

In Python 2, raw_input() returns a string, and input() tries to run the input as a Python expression.

Since getting a string was almost always what you wanted, Python 3 does that with input(). As Sven says, if you ever want the old behaviour, eval(input()) works.

您可以使用此网站学习打字 python

visualize python

python3

integer = int(input("Enter a positive integer: "))
character = str(input("Enter a character, e.g. 'a': "))

for i in range(integer):
    print(character)

python2

integer = int(input("Enter a positive integer: "))
character = raw_input("Enter a character, e.g. 'a': ")

for i in range(integer):
    print character