在不使用 python 中的内置函数的情况下使用用户输入范围和镜像字符串反转字符串

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

关于如何在范围函数中使用用户输入“停止”,然后在不获得双重用户输入的情况下镜像字符串(在本例中为字母表),我一直被这个小问题困扰。

另外,现在代码只能输入一个数字,但最好输入一个字母,将其转换为适合其在字母表中位置的正确数字。并将其用于射程! 但是我试过扭来扭去,但没有得到令人满意的结果。

感谢您的耐心等待!

 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)

输出示例:

Please enter a letter: 5
abcdeedcba

目标是获得以下内容:

Please enter a letter: "e"
abcdefgfedcba

请大家多多批评我是来学习的!

我也试过了

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)

输出:

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

目标是获得以下内容:

Please enter a letter: "g"
abcdefgfedcba

您似乎想向用户询问字母,然后将之前的字母与镜像一起列出。

试试这个代码

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)

输出

Please enter a letter: g
abcdefgfedcba