【问题标题】:Rock, Paper, Scissors game reset function石头剪刀布游戏重置功能
【发布时间】:2020-12-16 11:59:48
【问题描述】:

我是编程新手,一直在尝试掌握函数的窍门,并开始探索 Tkinter。我按照这个关于石头、纸、剪刀的教程,我有一个重置按钮,当用户点击它时,它会清除结果和用户输入的内容作为他们的选择,除了它似乎没有清除计算机的选择,即计算机选择rock,点击reset,电脑再次选择rock,reset,等等……所以我必须点击exit,然后运行程序才能得到不同的结果。

我尝试在def Reset()comp_pick.set("") 中分配comp_pick = None,因为我像这样清除了user_pick。我只是想弄清楚我错过了什么。

#import library
from tkinter import *
import random

#initialize window set up 
root = Tk()
root.geometry('500x500')
root.resizable(0,0)
root.title('DataFlair-Rock,Paper,Scissors')
root.config(bg ='pink3')


#heading set up 
Label(root, text = 'Rock, Paper ,Scissors' , font='arial 20 bold', bg = 'pink2').pack()


##user choice set up 
user_take = StringVar()
Label(root, text = 'choose any one: rock, paper ,scissors' , font='arial 15 bold', bg = 'pink2').place(x = 70,y=70)
Entry(root, font = 'arial 15', textvariable = user_take , bg = 'pink2').place(x=90 , y = 130)



#computer choice using the randint function
comp_pick = random.randint(1,3)
if comp_pick == 1:
    comp_pick = 'rock'
elif comp_pick ==2:
    comp_pick = 'paper'
else:
    comp_pick = 'scissors'
    



##function to play
Result = StringVar()

def play():
    user_pick = user_take.get()
    if user_pick == comp_pick:
        Result.set('It is a tie. You both have selected ' + user_pick +'.') 
    elif user_pick == 'rock' and comp_pick == 'paper':
        Result.set('You loose! Computer selected paper.')
    elif user_pick == 'rock' and comp_pick == 'scissors':
        Result.set('You win! Computer selected scissors.')
    elif user_pick == 'paper' and comp_pick == 'scissors':
        Result.set('You loose! Computer selected scissors.')
    elif user_pick == 'paper' and comp_pick == 'rock':
        Result.set('You win! Computer selected rock.')
    elif user_pick == 'scissors' and comp_pick == 'rock':
        Result.set('You loose! Computer selected rock.')
    elif user_pick == 'scissors' and comp_pick == 'paper':
        Result.set('You win! Computer selected paper.')
    else:
        Result.set('invalid: choose any one -- rock, paper, scissors')
    
        
    
##fun to reset
def Reset():
    Result.set("") 
    user_take.set("")
    comp_pick.set("")


##fun to exit
def Exit():
    root.destroy()


###### button
Entry(root, font = 'arial 10 bold', textvariable = Result, bg ='pink2',width = 50,).place(x=25, y = 250)

Button(root, font = 'arial 13 bold', text = 'PLAY'  ,padx =5,bg ='pink4' ,command = play).place(x=150,y=190)

Button(root, font = 'arial 13 bold', text = 'RESET'  ,padx =5,bg ='pink4' ,command = Reset).place(x=70,y=310)

Button(root, font = 'arial 13 bold', text = 'EXIT'  ,padx =5,bg ='pink4' ,command = Exit).place(x=230,y=310)

root.mainloop()

【问题讨论】:

    标签: python


    【解决方案1】:

    您在程序开始时设置comp_pick exactly once。相反,请在play 的开头执行此操作,以便在用户每次玩游戏时选择一个新的随机值。毕竟,每次这样你都已经选择了一个新的user_pick

    【讨论】:

      【解决方案2】:

      其他人指出了如何解决您所询问的问题。我想指出一件可能会导致未来出现问题的小事:

      comp_pick = random.randint(1,3)
      if comp_pick == 1:
          comp_pick = 'rock'
      elif comp_pick ==2:
          comp_pick = 'paper'
      else:
          comp_pick = 'scissors'
      

      在这里,您使用变量comp_pick 来表示两个不同的东西。首先它是一个介于 1 和 3 之间的整数值。然后将它重新分配给一个字符串值以供选择。像这样为两种不同的含义重用一个变量很容易导致错误,所以你应该避免这样做。相反,请使用两个不同的变量。

      【讨论】:

      • 好吧好吧,看看我的答案我不认为你会需要它
      【解决方案3】:

      因此,在您的代码中,您选择计算机的位置

      #computer choice using the randint function
      comp_pick = random.randint(1,3)
      if comp_pick == 1:
          comp_pick = 'rock'
      elif comp_pick ==2:
          comp_pick = 'paper'
      else:
          comp_pick = 'scissors'
      

      它在root.mainloop() 内,您必须知道.mainloop() 做了什么,您可以从here 中读取,但简而言之,这个主循环作为一种事件侦听器等,所以每当您重新设置小部件的分数时再次加载,并且您的计算机的选择也被选中,因为它在那里被执行。因此,在 mainloop 和 play() 函数之前创建一个函数并在需要时调用它

      def computer_choice():
          #computer choice using the randint function
          choices = ['rock', 'paper', 'scissors']
          comp_pick = random.choice(choices)
      

      并且在你需要的时候调用这个函数,所以你的整个代码应该是这样的:

      #import library
      from tkinter import *
      import random
      
      #initialize window set up 
      root = Tk()
      root.geometry('500x500')
      root.resizable(0,0)
      root.title('DataFlair-Rock,Paper,Scissors')
      root.config(bg ='pink3')
      
      
      #heading set up 
      Label(root, text = 'Rock, Paper ,Scissors' , font='arial 20 bold', bg = 'pink2').pack()
      
      
      ##user choice set up 
      user_take = StringVar()
      Label(root, text = 'choose any one: rock, paper ,scissors' , font='arial 15 bold', bg = 'pink2').place(x = 70,y=70)
      Entry(root, font = 'arial 15', textvariable = user_take , bg = 'pink2').place(x=90 , y = 130)
      
      
      ##function to play
      Result = StringVar()
      
      def computer_choice():
          #computer choice using the randint function
          choices = ['rock', 'paper', 'scissors']
          global comp_pick
          comp_pick = random.choice(choices)
      
      def play():
          computer_choice()
          user_pick = user_take.get()
          if user_pick == comp_pick:
              Result.set('It is a tie. You both have selected ' + user_pick +'.') 
          elif user_pick == 'rock' and comp_pick == 'paper':
              Result.set('You loose! Computer selected paper.')
          elif user_pick == 'rock' and comp_pick == 'scissors':
              Result.set('You win! Computer selected scissors.')
          elif user_pick == 'paper' and comp_pick == 'scissors':
              Result.set('You loose! Computer selected scissors.')
          elif user_pick == 'paper' and comp_pick == 'rock':
              Result.set('You win! Computer selected rock.')
          elif user_pick == 'scissors' and comp_pick == 'rock':
              Result.set('You loose! Computer selected rock.')
          elif user_pick == 'scissors' and comp_pick == 'paper':
              Result.set('You win! Computer selected paper.')
          else:
              Result.set('invalid: choose any one -- rock, paper, scissors')
          
              
          
      ##fun to reset
      def Reset():
          Result.set("") 
          user_take.set("")
          comp_pick.set("")
      
      
      ##fun to exit
      def Exit():
          root.destroy()
      
      
      ###### button
      Entry(root, font = 'arial 10 bold', textvariable = Result, bg ='pink2',width = 50,).place(x=25, y = 250)
      
      Button(root, font = 'arial 13 bold', text = 'PLAY'  ,padx =5,bg ='pink4' ,command = play).place(x=150,y=190)
      
      Button(root, font = 'arial 13 bold', text = 'RESET'  ,padx =5,bg ='pink4' ,command = Reset).place(x=70,y=310)
      
      Button(root, font = 'arial 13 bold', text = 'EXIT'  ,padx =5,bg ='pink4' ,command = Exit).place(x=230,y=310)
      
      root.mainloop()
      

      Jai Hind,Jai Bharat

      【讨论】:

      • 虽然您创建函数的建议是正确的,但您对“它在 root.mainloop() 内”的解释是不正确的。计算机选择的代码只运行一次,而不是连续运行。
      • 嗯,@Code-Apprentice 我已经更新了我的答案
      猜你喜欢
      • 2016-01-16
      • 2022-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多