python - 如何在python中打印变量“new_text”
问题描述
text_box 是一个 tkinter 文本框我正在尝试将 source_text 的字母转换为与所述字母的位置相对应的数字,使得 a 或 A = 1、b 或 B = 2 并将其打印到控制台
def enter():
#don't use 1 in place of 1.0
source_text=text_box.get(1.0, END)
def alphabet_position(text):
dict = {'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':'26'}
new_text = text.lower(source_text)
for i in new_text:
if i in dict:
new_text = new_text.replace(i, dict[i])
print (new_text)
输入是这样调用的
btn = Button(root, text = "Enter", command = enter)
解决方案
dict = {'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':'26'}
new_text = text.lower()
for i in new_text:
if i in dict:
return new_text.replace(i, dict[i])
def enter():
print (alphabet_position(text_box.get(1.0, END)))
推荐阅读
- python - 使用 pandas read_csv 导入数据:带有两行错误的标题
- django - 为什么当我单击“create.html”中的创建按钮时 django 模型无法保存我的新对象
- python - Python随机矩阵
- c# - 使用 Dbquery 实体框架显示两个关系 DB_table 之间的数据时发生错误
- angular - 使用角度材料拖放时,无线电输入选择重置
- python - 尝试使用 ctypes 设置回调时出现奇怪的访问冲突错误
- docker - Kubernetes mountPropagation 说明
- javascript - Highcharts - 如何使用 HTML 输入框输入饼图的数据?
- docker - Docker:如何在代码更改时更新容器
- java - 运行带有多个 Erlang 进程的 Java XML 解析器