简体   繁体   中英

Reversing a string while using user input for range and mirroring string without using builin functions in python

I have been stuck with this little problem on how to use the user input for "stop" in the range function and then mirror the string ( in this case the alphabet) back without getting double user input.

Plus now the code can only take a number but it woul be nice to enter a letter convert it to the right number for it's place in the alphabet. And use that for range! But I tried twisting and turning and im not getting agreeable results.

Thank you for your time and patience!

 def front_aplhabet():
    alphabet = ''
    for letter in range(0,  human):
        alphabet += chr(ord("a") + letter)
    return alphabet

# function to mirror front_aplhabet output
def back_alphabet(input):
    return input[::-1]


human = int((input("Please enter a letter: "))

palidrome_2 = front_aplhabet() + back_alphabet(front_aplhabet())

print(palidrome_2)

output example:

Please enter a letter: 5
abcdeedcba

The goal is to get the following:

Please enter a letter: "e"
abcdefgfedcba

Please be very critical I'm here to learn !

I have tried this too

def front_aplhabet():
    alphabet = ''
    for letter in range(0, int(goat)):
        alphabet += chr(ord("a") + letter)
    return alphabet

# function to mirror front_aplhabet output
def back_alphabet(input):
    return input[::-1]


human = (input("Please enter a letter: "))
goat = ord(human)

palidrome_2 = front_aplhabet() + back_alphabet(front_aplhabet())

print(palidrome_2)

output:

Please enter a letter: 5
abcdefghijklmnopqrstuvwxyz{|}~~}|{zyxwvutsrqponmlkjihgfedcba

The goal is to get the following:

Please enter a letter: "g"
abcdefgfedcba

It looks like you want to ask the user for a letter then list the previous letters along with the mirror.

Try this code

def front_alphabet(human):
    alphabet = ''
    for letter in range(0,  human):
        alphabet += chr(ord("a") + letter)
    return alphabet

# function to mirror front_aplhabet output
def back_alphabet(input):
    return input[::-1]


human = ord(input("Please enter a letter: ")) - 97  # convert lowercase letter to ascii

palidrome_2 = front_alphabet(human) + chr(human+97) + back_alphabet(front_alphabet(human))

print(palidrome_2)

Output

Please enter a letter: g
abcdefgfedcba

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM