0

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

for example

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

OUTPUT

a
a
a
a

This is what I currently have, but I get name error, ' ' is not defined, ' ' being the letter

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

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

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

If I typed 4, 4 it would give me

4
4
4
4

That works, but letters will not output, I'm new to python so you'll have to excuse me

Any ideas?

Link of Error: https://i.stack.imgur.com/dYtv8.jpg

Acryaz
  • 29
  • 2
  • 5

2 Answers2

1

Use raw_input for python 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

See this Stack Overflow question for explanation on input vs raw_input in 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.

Callam Delaney
  • 641
  • 3
  • 15
-1

You could use this website to learn typing 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
Tony Chou
  • 584
  • 1
  • 9
  • 26