3 回答

TA貢獻1826條經驗 獲得超6個贊
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()
另請注意,Python 2 現在正式不受支持,并且不再接收任何更新,包括安全補丁。今天學習 Python 的任何人都應該學習 Python 3。

TA貢獻1811條經驗 獲得超4個贊
了解什么是局部變量。最簡單(盡管很愚蠢)的解決方案是使用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()
希望這會有所幫助!

TA貢獻1820條經驗 獲得超2個贊
這里有很多可能的方法。與您當前的方法最相似的方法是傳遞一個函數。你有一個函數,像這樣:
paper()
函數可以有參數。這些參數將進入括號,它們成為該函數中的變量。舉個例子,你可以有一個函數 play(),然后像這樣向它傳遞 'rock'、'paper' 或 'scissors':
def play(user_choice):
if user_choice == 'paper':
paper()
if user_choice == 'rock':
rock()
....
然后,您將像這樣調用該函數:
>>> play('paper')
在該示例中,我將字符串“paper”傳遞給函數 play()。但是,我也可以將函數 play() 傳遞給函數,如下所示:
def play(func):
func()
通過這種方法,我可以將函數“rock”“paper”或“scissors”發送到函數 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 語句時,它會確定該語句是真還是假,只有在它為真時才運行其中的代碼。但是,與 if 語句不同的是,當它運行完代碼后,它會返回并再次重新評估該語句,如果它仍然為真,則再次運行它。僅當您更改其評估的變量以使語句最終評估為 false 時,while 循環才會結束,此時程序將繼續。
添加回答
舉報