![]() |
Need help with rpsls game(Beginner corner) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Need help with rpsls game(Beginner corner) (/thread-27276.html) |
Need help with rpsls game(Beginner corner) - Bulxy88 - Jun-01-2020 Hi,I tried to write the codes without an instruction so I've made lots of mistakes, but I would be thankful to get reviewed and find my mistakes. #Game:rpsls a="rock" b="Spock" c="paper" d="lizard" e="scissors" #So as the first step I need to assign those letters to their #corresponding numbers def letter_to_number(letter): if letter== a: number=0 elif letter== b: number=1 elif letter== c: number=2 elif letter== d: number=3 elif letter== e: number=4 return number def number_to_letter(number): if number==0: letter= a elif number==1: letter= b elif number==2: letter= c elif number==3: letter= d elif number==4: letter= e return letter #So in this step I'm gonna print player and computer choices #and then define how one of them is gonna win import random def game(player_choice): print "Player chooses" + player_choice player_choice=random.randrange(0, 5) return player_choice def game(computer_choice): print "Computer chooses" + computer_choice computer_choice=random.randrange(0, 5) return computer_choice #So I'm gonna use the difference to declare the winner. difference= (player_choice - computer_choice)%5 if difference == 1 or difference == 2: print "Player wins!" if difference == 2 or difference ==3: print "Computer wins!" else: print "Player and computer tie!" game("rock") game("Spock") game("paper") game("lizard") game("scissors") RE: Need help with rpsls game(Beginner corner) - pyzyx3qwerty - Jun-01-2020 Line 43,47,54,56,58 - missing parentheses in print statement line 52 - player_choice can be use only in the game() function, not outside it Edit: Here's a simplified version of your game #Game:rpsls #So as the first step I need to assign those letters to their #corresponding numbers import random def game(player_choice,computer_choice): if player_choice == "rock": number = 0 elif player_choice == "Spock": number = 1 elif player_choice == "paper": number = 2 elif player_choice == "lizard": number = 3 elif player_choice == "scissors": number = 4 if computer_choice == "rock": number2 = 0 elif computer_choice == "Spock": number2 = 1 elif computer_choice == "paper": number2 = 2 elif computer_choice == "lizard": number2 = 3 elif computer_choice == "scissors": number2 = 4 #So in this step I'm gonna print player and computer choices #and then define how one of them is gonna win print ("Player chooses" + player_choice) print ("Computer chooses" + computer_choice) #So I'm gonna use the difference to declare the winner. if number > number2 : difference= (number - number2) else: difference= (number2 - number) if difference == 1 or difference == 2: print ("Player wins!") if difference == 2 or difference ==3: print ("Computer wins!") else: print ("Player and computer tie!") comp_choice = random.choice(["rock","Spock","paper","lizard","scissors"]) game("rock",comp_choice) game("Spock",comp_choice) game("paper",comp_choice) game("lizard",comp_choice) game("scissors",comp_choice) |