本帖最后由 huangdongdong 于 2021-4-5 12:37 编辑 complex_font = []#繁体字
familiar_style = []#简体字
#去除空格
pompt = '''砵 妸 屙 讹 俄 娥 峨 莪 锇 鹅 蛾 额 婀 厄 阨 呃 扼 苊 轭 垩 恶 饿 谔 鄂 阏 愕
萼 遏 腭 锷 鹗 颚 噩 諤 鳄 恩 蒽 摁 儿 而 鸸 鲕 尔 耳 迩 洱 饵 珥 铒 二 佴 贰 唔 诶'''
str_complex = pompt
for each in str_complex:
if each == ' ':
str_complex.replace(each,' ')
else:
complex_font.append(each)
prompt = '''砵 妸 屙 訛 俄 娥 峨 莪 锇 鵝 蛾 額 婀 厄 阨 呃 扼 苊 轭 垩 惡 餓 谔 鄂 阏 愕
萼 遏 腭 锷 鹗 颚 噩 諤 鳄 恩 蒽 摁 兒 而 鸸 鲕 爾 耳 迩 洱 餌 珥 铒 二 佴 貳 唔 诶'''
str_familiar = prompt
for each in str_familiar:
if each == ' ':
str_familiar.replace(each,' ')
else:
familiar_style.append(each)
#简体转繁体
dict1 = {}
dict1 = dict(zip(complex_font,familiar_style))
def fun():
list_font = list_preserve
for each in list_font:
for each_font in complex_font:
if each in each_font:
print(dict1[each_font],end = '')
#繁体转简体
dict2 = {}
dict2 = dict(zip(familiar_style,complex_font))
def function():
list_style = list_preserve
for each in list_style:
for each_font in familiar_style:
if each in each_font:
print(dict2[each_font],end = '')
#判断字体
list_preserve = []
def font(content):
for each in content:
if each == ' ':
content.replace(each,' ')
else:
list_preserve.append(each)
for each_font in list_preserve:
if each_font in complex_font:
fun()
else:
function()
content = input('请输入一个字符串:')
font(content)
===================== RESTART: C:\Users\ASUS\Desktop\222.py ====================
请输入一个字符串:鲕爾耳迩洱餌珥铒二
鲕耳迩洱珥铒二鲕尔耳迩洱饵珥铒二鲕耳迩洱珥铒二鲕耳迩洱珥铒二鲕耳迩洱珥铒二鲕尔耳迩洱饵珥铒二鲕耳迩洱珥铒二鲕耳迩洱珥铒二鲕耳迩洱珥铒二
>>>
哪儿出错了
# -*- coding:utf-8 -*-
complex_font = []#繁体字
familiar_style = []#简体字
#去除空格
pompt = '''砵 妸 屙 讹 俄 娥 峨 莪 锇 鹅 蛾 额 婀 厄 阨 呃 扼 苊 轭 垩 恶 饿 谔 鄂 阏 愕
萼 遏 腭 锷 鹗 颚 噩 諤 鳄 恩 蒽 摁 儿 而 鸸 鲕 尔 耳 迩 洱 饵 珥 铒 二 佴 贰 唔 诶'''
str_complex = pompt
for each in str_complex:
if each == ' ':
str_complex.replace(each,' ')
else:
complex_font.append(each)
prompt = '''砵 妸 屙 訛 俄 娥 峨 莪 锇 鵝 蛾 額 婀 厄 阨 呃 扼 苊 轭 垩 惡 餓 谔 鄂 阏 愕
萼 遏 腭 锷 鹗 颚 噩 諤 鳄 恩 蒽 摁 兒 而 鸸 鲕 爾 耳 迩 洱 餌 珥 铒 二 佴 貳 唔 诶'''
str_familiar = prompt
for each in str_familiar:
if each == ' ':
str_familiar.replace(each,' ')
else:
familiar_style.append(each)
#简体转繁体
dict1 = {}
dict1 = dict(zip(complex_font,familiar_style))
def fun(font):
# list_font = list_preserve 这边不需要再次遍历,主函数之前已经有一个个判断并传入数值了
# for each in list_font:
# for each_font in complex_font:
if font in complex_font: # 判断这个字符是否在繁体列表,有的话就输出
print(dict1[font],end = '')
#繁体转简体
dict2 = {}
dict2 = dict(zip(familiar_style,complex_font))
def function(font):
#list_style = list_preserve
#for each in list_style:
#for each_font in familiar_style:
if font in familiar_style:
print(dict2[font],end = '')
else:
print("字典不匹配",end='')
#判断字体
list_preserve = []
def font(content):
for each in content:
if each == ' ':
content.replace(each,' ')
else:
list_preserve.append(each)
for each_font in list_preserve:
if each_font in complex_font:
fun(each_font) #如果这个字符存在繁体字典中,传入该字符
else:
function(each_font)
content = input('请输入一个字符串:')
font(content)
font 函数体内已经有遍历初始的字符串了,转化的函数体内又再次遍历判断,导致多次循环。
修改为font 函数遍历的时候将每个字符传入对应的函数。
|