Home > Software design >  Palimdrome(python) input giving than stops
Palimdrome(python) input giving than stops

Time:11-18

I have a program, but I want it develop it more to: That it reads a letter from input en print the string to this letter.

So if the input is b it will print aba. I have now a code that prints a til z and reverse of it... But i Don't know where to start now

a_y = ""

for val in range(97, 122):


    a_y  = chr(val)


print( a_y   'z'   a_y[::-1] )

CodePudding user response:

You just need to compare val with your "start character":

a_y = ""
start = 'd'
#start = input("Where to start from: ")

for val in range(97, 122):
    if chr(val) >= start:
        a_y  = chr(val)

print(a_y   'z'   a_y[::-1])

Out:

defghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfed

CodePudding user response:

you need to have a loop that will go to the inputed letter

you can do something like this:

a_y = input()
res = ""
i = 97
while i < ord(a_y):
    res  = chr(i)
    i = i   1
print( res   a_y   res[::-1] )

if input is for example d, output will be abcdcba

or

a_y = ""
end = input()

for val in range(97, 122):
    if val <= ord(end) - 1:
        a_y  = chr(val)

print(a_y   end   a_y[::-1])

in: d

out: abcdcba

Python ord() function returns the Unicode code from a given character.

For example, ord(‘a’) returns the integer 97

CodePudding user response:

Here is a potential approach:

from string import ascii_lowercase as letters

def palindrome(c):
    n = ord(c)-96
    if 1>n or n>len(letters) 1:
        print(f'invalid input "{c}"')
    s = letters[:n]
    return s s[-2::-1]

Example:

>>> palindrome('g')
'a'

>>> palindrome('g')
'abcdefgfedcba'

>>> palindrome('?')
invalid input "?"
  • Related