python - implementing functions in a game -
I have trouble with this part of my work, I have to declare the winner of the game and then input into the function Will happen. Once I have entered all the statements, I have to create a function def playGame ()
. Join it:
showRules () user = getUserChoice () computer = getComputerChoice () Announce winner (user, computer)
I also believe This is not how to do it. Please help.
Below is the code I have completed so far: (Do I have to make a statement for scissors?) #Display game rules print "Rule: Each player chooses a rock, paper, or scissor.) Print (" The winner is determined by the following rules: ") print (" cut paper in t \ t \ tcissors -> wins the scissors ") Print (" Deep User Choices:): Select = (Input ("Select (Rock, Paper, Paper, or Scissor).))). Lower () Return Choice # Desktops Computer input DEF computerchoice (): import random RNU = random. Rdid (1,3) if RNUM == 1: print ("computer selected rock.") If rnum == 2: print ("Computer Paper is selected. ") If rnum == 3: print (" Computer selected scissors ") Return RNM # Direct winner Def Deed DeWaldenner (user, RNUM): If User Choice == 1: print (" You Rock is selected. ") If GetComputerchoice == 1: print (" Computer I have also chosen a copy. ") Return 0 and print:" (The computer has selected the scissor, the rock scissors break down, you won! ") If userchoose == 2: print (" You have selected paper ") 1) Print ("The computer has chosen rock. Paper covers the rock! you've won! "Return" 1 Alif GetComputerchoice == 2: Print ("Computer has selected paper too.") Returns 0 and Returns: Print ("The computer has selected scissors. You have lost the scissor cut paper!") User 1 : 3 If print is printed ("You have selected scissors") if getComputerchoice == 1: print ("The computer has chosen rock to break rock scissor! You are lost!") 1 ALIF Ridge: Computer Chauz == 2: print ("computer selected paper. Scissors cut paper. Win! ") 1
how bout
The hand of the class: def __init __ (auto, RPS): itself RPS = ["rock", "paper", "scissor"]. Index (rps .flow ()) def __cmp __ (self, others): #profit Take the python scope __cmpd and override it # This magic is wins_against = {0: 2,1: 0,2: 1} # If their equal return 0 if self.rps == Other.RP: return 0 # If it wins the hand Returns 1 if wins_against [self.rps] == Other.RP: Returns # 1 Else (This hand is lost) Return -1Ri Enrichment -1 Diif __stret __ (self): return [ "rock", "paper", "scissors"] [itself. Print (hand ("rock")> hand ("paper")) print (arm ("scissor")> hand ("paper")) print (arm ("rock")> arm ("Scissors"))
For this, maybe it was overkill but this is a good technique ... and if you can get it down, then it will be done for future tasks. Def get_cpu_pick (): return random_chick ("rock", "paper", "scissor"]) def get_player_pick (): valid_inputs = {'r': ' Rock ',' p ':' paper ',' s': 'scissors'} user_choice = input (select "RPS:") while user_choice.lower () Valid_inputs is not in: user_choice = input (select "RPS:") valid_input back. Gate (user_choice.lower ()) def play (): user_hand = hand (get_player_pick ()) cpu_hand = hand (get_cpu_pick ()) if user_hand & gt; Cpu_hand: print ("User Wins {0} v. {1}". Format (user_hand, cpu_hand)) elif user_hand & lt; Cpu_hand: print ("CPU win {0} v. {1}". Format (user_hand, cpu_hand)) Other: print ("TIE !!!")
Comments
Post a Comment