【问题标题】:Issue with Wins and Losses... (must have easygui)输赢问题...(必须有easygui)
【发布时间】:2018-03-01 12:00:19
【问题描述】:
ComputerChoice = ""
x = 0
wins = 0
losses = 0
ties = 0
rounds = 0
abc = 0
CurrentStatus = 'started'

Choices = ['Rock','Paper','Scissors']
#################################################
def computerchoice(ComputerChoice, Choices, UserChoice): #number
          listthing = Choices[:]
          #listthing.remove[UserChoice]
          ComputerChoice = random.choice(listthing)
          ComputerChoice = Choices.index(ComputerChoice)
          return ComputerChoice
#################################################
import easygui
import random
easygui.msgbox( "Hello, This is a standard game of Rock, Paper, Scissors.","Welcome!","Next>>>")

while x == 0:
         UserChoice = easygui.buttonbox(' __ __ __ __ __ __ __ __ __ __ You just ' +CurrentStatus+ '. __ __ __ __ __ __ __ __ __ __ \n You currently have won '+str(wins)+ ' times, lost ' +str(losses)+' times, and tied '+ str(ties)+' times.  \n\n\nClick your next move: ','Choice Picker 2000',['Rock','Paper','Scissors','Done'])
         UserChoice = ['Rock','Paper','Scissors','Done'].index(UserChoice)
         ComputerChoice = computerchoice(ComputerChoice, Choices, UserChoice)
         if UserChoice == ComputerChoice:
                  ties = ties +1
                  rounds = rounds +1
                  CurrentStatus = "Tied"


         if UserChoice== 3:
                  x = 1
                  break

         elif UserChoice > ComputerChoice and UserChoice + ComputerChoice != 4:
                  wins = wins +1
                  rounds = rounds + 1
                  CurrentStatus = "Won"

         elif UserChoice < ComputerChoice and UserChoice + ComputerChoice != 4:
                  losses = losses +1
                  rounds = rounds +1
                  CurrentStatus = "Lost"

         elif UserChoice + ComputerChoice ==4 and UserChoice != ComputerChoice:
                  if Userchoice == 1:
                           score = score +1
                           rounds = rounds +1
                           CurrentStatus = "Won"
                  elif ComputerChoice == 1:
                          losses = losses +1
                          rounds = rounds +1

                          CurrentStatus = "Lost"


result = ["Cool.","Okay.","I am a failure"]
if wins>losses:
         easygui.msgbox("You won "+str(wins)+ " times, lost " +str(losses)+" times, tied "+ str(ties)+ " and won " +str(int(float(wins)/float(rounds)*100))+ "% of the time.","",result[0])
elif wins==losses:
         easygui.msgbox("You won "+str(wins)+ " times, lost " +str(losses)+" times, tied "+ str(ties)+ " and won " +str(int(float(wins)/float(rounds)*100))+ "% of the time.","",result[1])
elif wins<losses:
         easygui.msgbox("You won "+str(wins)+ " times, lost " +str(losses)+" times, tied "+ str(ties)+ " and won " +str(int(float(wins)/float(rounds)*100))+ "% of the time.","",result[2])

当我运行它时,它工作正常,但如果你按“摇滚”,那么你将永远平局/输球,永远不会赢。如果您按“剪刀”,那么您将永远平局/赢,永远不会输。 我很确定这是同一个问题,但如果有人可以查看它,我将非常感激。

【问题讨论】:

    标签: python easygui


    【解决方案1】:

    您确定获胜者的逻辑似乎有些复杂。或许可以尝试将其中的一些简化并提取到辅助函数中。

    (此函数故意不必要地冗长)

    def userWon(userChoice, computerChoice):
        if (userChoice == (computerChoice + 1 % 3)):
            return True
        if (computerChoice == (userChoice + 1 % 3)):
            return False
        if (computerChoice == userChoice):
            return None
    

    请注意,您的剪刀石头布列表的顺序是,对于位置c 的任何给定选择,击败位置c + 1 mod 3 的选择。使用它,您可以使用这个辅助函数,如果用户赢了,则返回 True,如果输了,则返回 False,如果是平局,则返回 None

    然后你可以在调用它之前检查退出选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      相关资源
      最近更新 更多