Skip to content
Advertisement

make a list and limit only the input in single character using python.if the user input two characters it will be invalid

a = []

for i in range (5):
    a.append(input ("Enter only one character: "))
a.sort()
print(a)

a.reverse()
print(a)

This is my code and I don’t know how to detect a single character. If a user input two character they will get an error but if the user input single character the input item will be in the list.

Advertisement

Answer

a = []

for i in range (5):
    c = input ("Enter only one character: ")
    if len(c) == 1:
        a.append(c)
    else:
        raise ValueError("Input is not a single character")
a.sort()
print(a)

a.reverse()
print(a)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement