- Rock Paper Scissors in Python
- Python Coding Rock,Paper,Scissors,Lizard and Spock [duplicate]
- 1 Answer 1
- Saved searches
- Use saved searches to filter your results more quickly
- pablorgarcia/rock-paper-scissors-lizard-spock
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
Rock Paper Scissors in Python
I am trying to write a Python program and I am having a hard time getting my score. I have written it as a value returning function and every time I run the program it seems to skip the step where it retrieves the score unless I include an else statement which it will automatcially jump the the else statement. I will attach the full code below. Thank you very much for any help, I’m greatful! This is also my first time posting in this forum I apologize if I screw something up.
#constants Rock = 1 Paper = 2 Scissors = 3 #Define the main function def main(): #set control loop keep_going = 'Y' #set counter to zero computer_wins = 0 player_wins = 0 tie_score = 0 #call display message display_message() while keep_going == 'y' or keep_going == 'Y': play_game() #prompt user to keep going keep_going = input('would you like to play again? (Y for Yes): ') print('The computer won', computer_wins, 'times') print('The player won', player_wins, 'times') print('There were', tie_score, 'tie scores') def play_game(): #get random input computer = get_random() #get the players input play = get_play() #validate input if play == '1' or play == '2' or play == '3': play == True else: play == False print('Error: Invalid Entry') play = input('Please enter 1 for Rock, 2 for Paper, or 3 for Scissors: ') if play == computer: print('Tie Score, Please try again') tie_score += 1 else: get_score(computer, play) print('The computer chose:', computer) print('The player chose: ', play) #define display message def display_message(): print('Welcome to Rock Paper Scissors, a game of chance to see who will') print('outsmart the other. This game is Man VS Computer.') print('The program will select a random integer and then ask you for an integer') print('1 for Rock 2 for paper or 3 for Scissors. The program will then tell') print('you who won the game.') print('GOOD LUCK!') print print def get_random(): import random #generate random int computer = random.randint(1, 3) return computer def get_play(): #prompt user to enter an integer 1, 2, or 3 play = input('Select 1 for Rock, 2 for Paper, or 3 for Scissors: ') return play def get_score(computer, play): if computer == 1 and play == 2: score = 'player wins' print('Paper covers Rock, Player Wins') #player wins player_wins += 1 elif computer == 1 and play == 3: score = 'computer wins' print('Scissors cut Paper, Computer Wins') #computer wins computer_wins += 1 elif computer == 2 and play == 1: score = 'computer wins' print('Paper covers Rock, Computer Wins') #computer wins computer_wins += 1 elif computer == 2 and play == 3: score = 'player wins' print('Scissors cut Paper, Player Wins') #player wins player_wins += 1 elif computer == 3 and play == 1: score = 'player wins' print('Rock smashes Scissors, Player Wins') #player wins player_wins += 1 elif computer == 3 and play == 2: score = 'computer wins' print('Scissors cut Paper, Computer Wins') #computer wins computer_wins += 1 #call main function main()
Python Coding Rock,Paper,Scissors,Lizard and Spock [duplicate]
And the error you are receiving is due to the fact that you are trying to concatenate a str and an int . I would recommend using print(‘ ‘.join(( user_name, » you have won «, wins, » games»))) or print(user_name + » you have won » + str(wins) + str(games)) it’s up to you but I will tell you that .join() is faster.
1 Answer 1
This could be shortened heavily but I don’t feel like rewriting your entire program. This should work as expected.
total_guess = 0 wins = 0 loss = 0 import random characters = ["rock", "paper", "scissors", "lizard", "spock"] computer = characters[random.randint(0,4)] print(computer) def valid(text, flag): error_message= "" while True: var = input(error_message + text) if flag == "s": if var.isalpha()==True: break else: error_message = "This is not valid, " elif flag =="i": if var.isdigit()==True: var = int(var) break else: error_message = user_name + " this is not a number, " elif flag == "g": if var == "rock" or var == "paper" or var == "scissors" or var == "lizard" or var == "spock": break else: error_message = user_name + " this is not valid! " return(var) user_name = valid("What is your name?", "s") num_rounds = valid(user_name +" how many rounds do you want?", "i") while True: while num_rounds > total_guess: player = valid(user_name + """ ,What do you want as your character: Rock, paper, scissors, lizard or spock""", "g" ) total_guess = total_guess + 1 if player == computer: print("Draw!") # -------------------------------------------- elif player == "Rock" or player == "rock": if computer == "paper" or computer == "spock" : loss = loss + 1 print(' '.join(("You lost", computer, "beats", player))) if computer == "scissors" or computer == "lizard": wins = wins + 1 print(' '.join(("You win", player, "beats", computer))) elif player == "Paper" or player == "paper": if computer == "scissors" or computer == "lizard": loss = loss + 1 print(' '.join(("You lost", computer, "beats", player))) if computer == "rock" or computer == "spock": wins = wins + 1 print(' '.join(("You win", player, "beats", computer))) elif player == "Scissors" or player == "scissors": if computer =="Spock" or computer == "rock": loss = loss + 1 print(' '.join(("You lost", computer, " beats ", player))) if computer =="paper" or computer == "lizard": wins = wins + 1 print(' '.join(("You win", player, "beats", computer))) elif player == "Lizard" or player =="lizard": if computer =="scissors" or computer == "rock": loss = loss + 1 print(' '.join(("You lost", computer, "beats", player))) if computer == "paper" or computer == "spock": wins = wins + 1 print(' '.join(("You win", player, "beats", computer))) elif player == "Spock" or player == "spock": if computer == "lizard" or computer == "paper": loss = loss + 1 print(' '.join(("You lost", computer, "beats", player))) if computer =="rock" or computer == "scissors": wins = wins + 1 print(' '.join(("You win", player, "beats", computer))) end_game = input("To exit enter N, to play again enter any key ") if end_game == 'n' or end_game == 'N': print("THANKS FOR PLAYING " + user_name + '!') break total_guess = 0
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
pablorgarcia/rock-paper-scissors-lizard-spock
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Rock paper scissors lizard Spock game
Stars, Forks and Pull Requests are welcome 🙂
It is first used to settle a dispute about what to watch on TV between Sheldon and Raj in «The Lizard-Spock Expansion». It is mentioned again in «The Euclid Alternative» and «The Rothman Disintegration», where Sheldon explains the rules to Penny and Barry Kripke.
The game was originally created by Sam Kass with Karen Bryla. According to an interview with Kass, the series producers did not originally ask for permission to use the game, but Kass was officially referenced by Sheldon as the creator of the game during the «The Rothman Disintegration», after which he states, «Hail Sam Kass!» to which Leonard, Howard, Raj, and Sheldon all then chant «Hail!» while raising their hands.
The game is an expansion on the game Rock, Paper, Scissors. Each player picks a variable and reveals it at the same time. The winner is the one who defeats the others. In a tie, the process is repeated until a winner is found. Almost always, the boys will all pick Spock at the same time and tie over and over again.
Mini-project development process requirements
Build a helper function name_to_number(name) that converts the string name into a number between 0 and 4 as described above. This function should use a sequence of if/elif/else clauses. You can use conditions of the form name == ‘paper’, etc. to distinguish the cases. To make debugging your code easier, we suggest including a final else clause that catches cases when name does not match any of the five correct input strings and prints an appropriate error message. You can test your implementation of name_to_number() using this name_to_number testing template. (Also available in the Code Clinic tips thread). Next, you should build a second helper function number_to_name(number) that converts a number in the range 0 to 4 into its corresponding name as a string. Again, we suggest including a final else clause that catches cases when number is not in the correct range. Implement the first part of the main function rpsls(player_choice). Print out a blank line (to separate consecutive games) followed by a line with an appropriate message describing the player’s choice. Then compute the number player_number between 0 and 4 corresponding to the player’s choice by calling the helper function name_to_number() using player_choice. Implement the second part of rpsls() that generates the computer’s guess and prints out an appropriate message for that guess. In particular, compute a random number comp_number between 0 and 4 that corresponds to the computer’s guess using the function random.randrange(). Then compute the name comp_choice corresponding to the computer’s number using the function number_to_name() and print an appropriate message with the computer’s choice to the console. Implement the last part of rpsls() that determines and prints out the winner. Specifically, compute the difference between comp_number and player_number taken modulo five. Then write an if/elif/else statement whose conditions test the various possible values of this difference and then prints an appropriate message concerning the winner. If you have trouble deriving the conditions for the clauses of this if/elif/else statement, we suggest reviewing the «RPSLS» video which describes a simple test for determine the winner of RPSLS. This will be the only mini-project in the class that is not an interactive game. Since we have not yet learned enough to allow you to play the game interactively, you will simply call your rpsls function repeatedly in the program with different player choices. You will see that we have provided five such calls at the bottom of the template. Running your program repeatedly should generate different computer guesses and different winners each time. While you are testing, feel free to modify those calls, but make sure they are restored when you hand in your mini-project, as your peer assessors will expect them to be there.
The output of running your program should have the following form: Player chooses rock Computer chooses scissors Player wins!
Player chooses Spock Computer chooses lizard Computer wins! Player chooses paper Computer chooses lizard Computer wins! Player chooses lizard Computer chooses scissors Computer wins! Player chooses scissors Computer chooses Spock Computer wins!