Reputation: 15
so i want to do the same with alphabets that i did with numbers take a look
num1=int(input("enter Ist digit:"))
num2=int(input("enter IInd digit:"))
num3=int(input("enter IIIrd digit:"))
num4=int(input("enter IVth digit:"))
num5=int(input("enter Vth digit:"))
i want to enter letters instead of numbers so i changed it to
alpha1=chr(input("enter Ist letter"))
but i kept on getting the error
alpha1=chr(input("enter Ist alphabet:"))
TypeError: an integer is required (got type str)
Upvotes: 2
Views: 6951
Reputation: 5591
Python consider standard input as string and in the first case it convert the string to integer. But in the second case it will not work as it get input as string and chr(param) always expect an integer value where param should be a integer value (0-9) . So just use like below:-
alpha1=(input("enter Ist alphabet:"))[0]
Upvotes: 0
Reputation: 20472
Read the docs:
chr(i)
Return a string of one character whose ASCII code is the integer i.
and
input([prompt])
If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that
you don't need to convert the character. What you get from input is already a string
Upvotes: 5