gpt4 book ai didi

python - 我在从输入框的输入创建变量时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:24:49 25 4
gpt4 key购买 nike

基本上,我正在尝试创建 Mad Lob。我的麻烦是,当我从函数中的输入创建变量时,我的语法表明我尚未定义 MN1 (我的第一个变量),这对我的其他变量来说也是如此。

from tkinter import *

root = Tk()
root.title("Mad Libs")

root.geometry("500x600")

x = 0

inp = None

def mLib():
global x
if x == 0:
lbl1["text"]="Man's Name"#1
MN1 = raw_input(v.get())
elif x == 1:
lbl1["text"]="Occupation"#2
O1 = raw_input(v.get())
elif x == 2:
lbl1["text"]="Noun"#3
N1 = raw_input(v.get())
elif x == 3:
lbl1["text"]="Noun"#4
N2 = raw_input(v.get())
elif x == 4:
lbl1["text"]="Noun"#5
N3 = raw_input(v.get())
elif x == 5:
lbl1["text"]="Shape"#6
S1 = raw_input(v.get())
elif x == 6:
lbl1["text"]="Man's Name"#7
MN2 = raw_input(v.get())
elif x == 7:
lbl1["text"]="Verb"#8
V1 = raw_input(v.get())
elif x == 8:
lbl1["text"]="Woman's Name"#9
WN1 = raw_input(v.get())
elif x == 9:
lbl1["text"]="Body Part"#10
BP1 = raw_input(v.get())
elif x == 10:
lbl1["text"]="Verb"#11
V2 = raw_input(v.get())
elif x == 11:
lbl1["text"]="Noun"#12
N4 = raw_input(v.get())
elif x == 12:
lbl1["text"]="Noun"#13
N5 = raw_input(v.get())
elif x == 13:
lbl1["text"]="Restaurant Name"#14
RN1 = raw_input(v.get())
elif x == 14:
lbl1["text"]="Historic Monument"#15
HM1 = raw_input(v.get())
elif x == 15:
lbl1["text"]="Verb Ending In ED"#16
V3 = raw_input(v.get())
elif x == 16:
lbl1["text"]="Noun"#17
N6 = raw_input(v.get())
elif x == 17:
lbl1["text"]="Noun"#18
N7 = raw_input(v.get())
elif x == 18:
lbl1["text"]="Noun"#19
N8 = raw_input(v.get())
elif x == 19:
lbl1["text"]="Verb"#20
V4 = raw_input(v.get())
elif x == 20:
lbl1["text"]="Noun"#21
N9 = raw_input(v.get())
elif x == 21:
lbl1["text"]="Adjective"#22
A1 = raw_input(v.get())
elif x == 22:
lbl1["text"]="Adjective"#23
A2 = raw_input(v.get())
elif x == 23:
lbl1["text"]="Emotion"#24
E1 = raw_input(v.get())
elif x == 24:
lbl1["text"]="Verb Ending In Ing"#25
V5 = raw_input(v.get())
elif x == 25:
lbl1["text"]="Noun"#26
N10 = raw_input(v.get())
elif x == 26:
lbl1["text"]="Noun"#27
N11 = raw_input(v.get())
elif x == 27:
lbl1["text"]="Verb"#28
V6 = raw_input(v.get())
else:
print(Para % (MN1, O1, N1, N2, N3, S1, MN2, V1, WN1, BP1, V2, N4, N5, RN1, HM1, V3, N6, N7, N8, V4, N9, A1, A2, E1, V5, N10, N11, V6))

inp = MN1, O1, N1, N2, N3, S1, MN2, V1, WN1, BP1, V2, N4, N5, RN1, HM1, V3, N6, N7, N8, V4, N9, A1, A2, E1, V5, N10, N11, V6



x = x+1
return

Para = '''%s is a normal %s. Then, one day, a %s explodes, causing a %s to blow up, and a nearby %s erupts into a %s of flames.
%s realizes that he's being chased by the government, who's trying to %s him. While on the run, he teams up with an incredibly
attractive woman named %s, who has an incredible %s. She may be from the streets, but she can %s like nobody's buisness. The
duo decide to turn tables on their pursuers by blowing up a %s, which triggers a chain reaction, causing the local %s, %s, and
%s to explode. Then, the bad guys' helicopter gets %s by a piece of %s from when the %s exploded, and the helicopter explodes
and falls onto a %s, causing it to %s, which shoots a fireball straight into the heart of %s and destroys the bad guy leader.
Everything is %s and the two decide that such a %s ordeal has caused them to fall in %s with each other. They decide to celebrate
by %s on the %s,and they even managed to use a %s from the beginning of the movie, to %s the whole story together.'''

print(Para % (MN1, O1, N1, N2, N3, S1, MN2, V1, WN1, BP1, V2, N4, N5, RN1, HM1, V3, N6, N7, N8, V4, N9, A1, A2, E1, V5, N10, N11, V6))



btn1 = Button(root,text="Enter", command=mLib)
btn1.pack()

lbl1 = Label(root, text="Lab")
lbl1.pack()
v = Entry(root).pack()

root.mainloop()

此外,当我的语法出现时,它还会突出显示 root.mainloop() 中的 root

最佳答案

在函数mlib之外声明您的变量,例如 MN1,并且根据您的代码,您没有从 eBox Entry 小部件中获取任何输入条目,而是从未知变量中获取它 v

关于python - 我在从输入框的输入创建变量时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38089475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com