python - NameError: name 'Pick' is not defined
问题描述
I am a learning the if statement by creating a rock, paper, scissors game with Tkinter. I'm trying to make a label that will appear if a button is pressed. so for example it will put a label saying "rock" if i press the rock button. But I have a problem in the if statement. This is what I have
import random
from tkinter import *
click = True
def compick():
choice = random.choice(["rock","paper","scissors"])
return choice
compchoice = compick()
def yourChoice(Pick):
global click
br = Button(gui, image=img1, command= lambda:yourChoice('rock'))
br.place(x=15, y=100)
bp = Button(gui, image=img2, command= lambda:yourChoice('paper'))
bp.place(x=200 ,y=100)
bs = Button(gui, image=img3, command= lambda:yourChoice('scissors'))
bs.place(x=350, y=100)
if click==True:
if Pick =='rock':
LR.place(x=225, y=500)
if compchoice =='rock':
LR.place(x=225, y=15)
gui.mainloop()
It gave me an error saying "name 'Pick' is not defined". I have no idea what's wrong with the code.
解决方案
Seems like an indentation error. Place all your code for the function yourChoice
under a single indent.
Something like this:
def yourChoice(Pick):
global click
br = Button(gui, image=img1, command= lambda:yourChoice('rock'))
br.place(x=15, y=100)
bp = Button(gui, image=img2, command= lambda:yourChoice('paper'))
bp.place(x=200 ,y=100)
bs = Button(gui, image=img3, command= lambda:yourChoice('scissors'))
bs.place(x=350, y=100)
if click==True:
if Pick =='rock':
LR.place(x=225, y=500)
if compchoice =='rock':
LR.place(x=225, y=15)
Now, all of the above mentioned code is executed when a call is made to the yourChoice
function.
推荐阅读
- asp.net-core - 带有 ASP.Net Core 和 Razor 页面的 Azure 存储文件共享
- linux - 自动登录和启动应用程序 - 终端信息亭
- jquery - jQuery 脚本在现场运行失败,但正在暂存。无法确定原因
- python - 让 mod-wsgi 运行使用 python3 编写的应用程序时出现问题
- mod-rewrite - 强制 RewriteRule 结束 htaccess 文件中的处理/循环
- json - 如何使用 get_json_object 从 json 字符串中检索多个 json 字段
- sql - 如何在没有子查询的情况下做到这一点?
- pandas - Python:如何在类外使用构建在类内的数据框?
- php - 错误用户在 Heroku 上上传文件但不是本地文件
- flutter - 寻找有关实施状态以从 ListView 添加/删除的建议