Home > front end >  Take number from user. For example if user enter 123 then it will print abc if user enter 13 12 11 t
Take number from user. For example if user enter 123 then it will print abc if user enter 13 12 11 t

Time:01-24

Take number from user. For example if user enter 123 then it will print abc if user enter numbers 13 12 11 then output would be 'mlk' using python language.

Please ! Help me to solve out this question.

list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
         'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

for i in enumerate((list1)):
    inputnum = int(input("enter number"))
    print(i)


Output  would be:
  (0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')
(4, 'e')
(5, 'f')
(6, 'g')
(7, 'h')
(8, 'i')
(9, 'j')
(10, 'k')
(11, 'l')
(12, 'm')
(13, 'n')
(14, 'o')
(15, 'p')
(16, 'q')
(17, 'r')
(18, 's')
(19, 't')
(20, 'u')
(21, 'v')
(22, 'w')
(23, 'x')
(24, 'y')
(25, 'z')

expected: If User enter 234 it will print cde as output

CodePudding user response:

Based on using an input(), I can suggest a following solution.

list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
         'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']


inputnum_list = []
while True:
    value_given = input("enter number")
    if value_given == '':
        output = [list1[letter_position] for letter_position in inputnum_list]
        print(''.join(output))
        inputnum_list = []
        continue
    inputnum_list.append(int(value_given))

It will take string inputs punctuated by "enter" presses and will output a list of letters after a blank value is entered.

CodePudding user response:

you can store user input as a list. and create a dictionary

usr_input=list(map(int,input("Enter the number:").split())) #User input stored as a list by using mapping

dic={1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f', 7:'g', 8:'h', 9:'i', 10:'j', 11:'k', 12:'l',13:'m', 14:'n', 15:'o', 16:'p', 17:'q', 18:'r', 19:'s', 20:'t', 21:'u', 22:'v', 23:'w', 24:'x', 25:'y', 26:'z'}

res=""
for num in usr_input:
    res =dic.get(num,'') #If the value is not present in a dictionary than add empty string to the res

print(res)

Output:

Enter the number:11 13 27 23
kmw

As 11:'k', 13:'m', 27:'' [as an empty string], 23:'w' -> 'k' 'm' '' 'w' ->'kmw'

Let's say you want to optimize space complexity [i.e without dictionary].

usr_input=list(map(int,input("Enter the number:").split()))

res=""
for num in usr_input:
    if 97<=num 96<=122: #ascii values of 'a':97 'z':122
        res =chr(num 96)

print(res)

Output:

Enter the number:11 13 27 16
kmp
  • Related