Python 如何调用一组函数

Python 如何调用一组函数,python,function,tkinter,Python,Function,Tkinter,我不知道如何做到每次按下按钮时,结果标签都会显示新的结果,我应该复制并粘贴重复功能中的3个石头、布和剪刀功能还是什么 它的python 2.7 from Tkinter import* import random # variables a = "rock" b = "paper" c = "scissors" pc = random.choice([a, b, c]) windows = Tk() windows.geometry("200x300") # function that m

我不知道如何做到每次按下按钮时,结果标签都会显示新的结果,我应该复制并粘贴重复功能中的3个石头、布和剪刀功能还是什么

它的python 2.7


from Tkinter import*
import random
# variables
a = "rock"
b = "paper"
c = "scissors"
pc = random.choice([a, b, c])
windows = Tk()
windows.geometry("200x300")


# function that makes the

def repeat():
    pc = random.choice([a, b, c])
    print pc
    # im stuck here!





def tie():
    lbresult.configure(text="Tie!")



def win():
    lbresult.configure(text="you win!!")



def loose():
    lbresult.configure(text="you loose")



# functions for each election
def rock():
    if pc == "paper":
        loose()
    elif pc == "rock":
        tie()
    elif pc == "scissors":
        win()
    repeat()


def paper():
    if pc == "paper":
        tie()
    elif pc == "rock":
        win()
    elif pc == "scissors":
        loose()
    repeat()


def scissors():
    if pc == "paper":
        win()
    elif pc == "rock":
        loose()
    elif pc == "scissor":
        tie()
    repeat()


#buttons for choosing options
lbresult = Label(text="result will appear here")
btrock = Button(text="  rock    ", command=rock)
btpaper = Button(text="  paper  ", command=paper)
btscissors = Button(text="scissors", command=scissors)

btrock.pack()
btpaper.pack()
btscissors.pack()
lbresult.pack()
#result label


windows.mainloop()
请帮我解决这个问题我快发疯了


如果你能回答,请解释为什么。。。我会非常感激的

了解局部变量是什么。最简单(尽管很愚蠢)的解决方案是使用
global
关键字,如下所示:

def repeat():
    global pc
    pc = random.choice([a, b, c])
    print(pc)
但是,如果代码应该是可靠的或可开发的,则千万不要使用
global
。以下是没有它的解决方案,请学习OOP以了解:

from tkinter import *
import random

class RPS:
    def __init__(self):
        self.windows = Tk()
        self.windows.geometry("200x300")

        self.a = "rock"
        self.b = "paper"
        self.c = "scissors"
        self.repeat()

        self.lbresult = Label(text="result will appear here")
        btrock = Button(text="  rock    ", command=self.rock)
        btpaper = Button(text="  paper  ", command=self.paper)
        btscissors = Button(text="scissors", command=self.scissors)

        btrock.pack()
        btpaper.pack()
        btscissors.pack()
        self.lbresult.pack()

        self.windows.mainloop()
    def repeat(self):
        self.pc = random.choice([self.a, self.b, self.c])
        print(self.pc)
    def tie(self):
        self.lbresult.configure(text="Tie!")
    def win(self):
        self.lbresult.configure(text="you win!!")
    def loose(self):
        self.lbresult.configure(text="you loose")
    def rock(self):
        if self.pc == "paper":
            self.loose()
        elif self.pc == "rock":
            self.tie()
        elif self.pc == "scissors":
            self.win()
        self.repeat()
    def paper(self):
        if self.pc == "paper":
            self.tie()
        elif self.pc == "rock":
            self.win()
        elif self.pc == "scissors":
            self.loose()
        self.repeat()
    def scissors(self):
        if self.pc == "paper":
            self.win()
        elif self.pc == "rock":
            self.loose()
        elif self.pc == "scissor":
            self.tie()
        self.repeat()
RPS()

希望对你有帮助

当且仅当按下按钮时,选择一个新的随机
pc
选项

...
c = "scissors"
pc = random.choice([a, b, c]) # remove this
windows = Tk()
...

...
def rock():
    pc = random.choice([a, b, c])
    if pc == "paper":
        loose()
    elif pc == "rock":
        tie()
    elif pc == "scissors":
        win()
    repeat() # remove this, and the whole repeat function itself
...
请注意,如果您只是想显示赢/输/平的最终结果,那么游戏本身并不需要任何逻辑。只需将所有三个按钮连接到显示结果的相同功能:

from Tkinter import *
import random
windows = Tk()
windows.geometry("200x300")

def play():
    lbresult.configure(text=random.choice(['You win!', 'You lose!', 'Tie!']))

lbresult = Label(text="result will appear here")
btrock = Button(text="  rock    ", command=play)
btscissors = Button(text="scissors", command=play)
btpaper = Button(text="  paper  ", command=play)

btrock.pack()
btpaper.pack()
btscissors.pack()
lbresult.pack()
#result label

windows.mainloop()

还请注意,Python2现在正式不受支持,不再接收任何更新,包括安全补丁。今天学习Python的人都应该学习Python 3。

这里有许多可能的方法。与当前方法最相似的方法是通过传递函数。您有一个函数,如下所示:

paper()
def play(user_choice):
  if user_choice == 'paper':
    paper()
  if user_choice == 'rock':
    rock()
....
>>> play('paper')
def play(func):
  func()
函数可以有参数。这些参数将进入括号,它们将成为该函数中的变量。例如,您可以使用一个函数play(),并将“石头”、“布”或“剪刀”传递给它,如下所示:

paper()
def play(user_choice):
  if user_choice == 'paper':
    paper()
  if user_choice == 'rock':
    rock()
....
>>> play('paper')
def play(func):
  func()
然后您可以这样调用该函数:

paper()
def play(user_choice):
  if user_choice == 'paper':
    paper()
  if user_choice == 'rock':
    rock()
....
>>> play('paper')
def play(func):
  func()
在该示例中,我将字符串“paper”传递给函数play()。但是,我也可以将函数play()传递给函数,如下所示:

paper()
def play(user_choice):
  if user_choice == 'paper':
    paper()
  if user_choice == 'rock':
    rock()
....
>>> play('paper')
def play(func):
  func()
通过这种方法,我可以将函数“rock”、“paper”或“Scissor”发送到函数play,然后调用我传递的任何函数。您可以(尽管这可能不是最好的方法)将一个函数传递给其内部的另一个函数:

def repeat(func):
  func()

def play():
  # do your things
  repeat(play)
但是。。这种做法是多余的。创建函数只是为了调用函数没有意义,而您可以简单地调用函数:

def play():
  # do your things
  play()
这是一个递归函数。在函数结束时,它再次调用自身。如果不停止它,这将永远持续下去,因此递归通常是有条件地编写的:

def play():
  # do your things
  continue = input("Would you like to continue [y/n]? ")
  if continue == 'y':
    play()
  else:
    pass    
    # This else statement is implicit. If you don't add it, it'll do it anyway.
另一个选项是循环,如“while循环”:

def play():
  keep_going = True
  while keep_going:
    #do your things
    continue = input("Would you like to continue [y/n]? ")
    if continue == "y":
      keep_going = True
    else:
      keep_going = False
while循环类似于递归if语句。当代码到达while语句时,它将确定该语句是true还是false,如果为true,则只在中运行代码。不过,与if语句不同的是,当它运行完代码后,它将返回并再次重新计算该语句,如果仍然为true,则再次运行它。while循环只有在您更改它计算的变量,使语句最终计算为false时才会结束,此时程序将继续