Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rock,paper,scissors
#1
I need to make this classic game.
my problem is that i dont know how to compare them to each other.
Doh
maybe something like this?
if user1 = rock and user2 = rock:
   print()
if user1 = rock and user2 = paper
   print()
#etc 
#etc
or is there a faster way?
Reply
#2
Comparison is done by double ==, assignment is one =
Reply
#3
(Dec-04-2016, 03:07 PM)Yoriz Wrote: Comparison is done by double ==, assignment is one =

ha yeah i know that, i made a typo, my bad LOL

so the only way to make this work is to == all of them?
Reply
#4
Yeah, making a few comparisons is probably the simplest way to go.
Do note that a simple user1 == user2 can be used to check for a draw.
Reply
#5
There are other ways to do it. One is to make a dictionary:

wins = {'rock': 'scissors', 'scissors': 'paper', 'paper': 'rock'}
Then you just need to test three things: if it matches the dictionary it's a win, if they played the same thing it's a draw, otherwise it's a loss.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
You could use
if (user1, user2) == ('rock', 'rock'):
Reply
#7
Done! Tongue  how would you have done it?

import random


def sub(computer):
    if computer == 1:
        computer = "Foot"        
    elif computer == 2:
        computer = "Nuke"
    elif computer == 3:
        computer = "Cockroach"
    print("Computer chose: ",computer)
    
    

def main():
    wins = 0
    rounds = 0
    draws = 0
    
    
    while True:
        computer = random.randint(1,3)
        player = input("Foot, Nuke or Cockroach? (Quit ends): ")
        
        if player == "Quit":
            print("You played",rounds, "rounds, and won",wins,"rounds, playing tie in",draws,"rounds.")
            break
        elif player == ("Spaceshuttle"):
            print("Incorrect selection.")
        else:
            print("You chose: ", player)
            sub(computer)
            rounds += 1
        
            if (player, computer) == ("Foot", 1):
                draws += 1
                print("It is a tie!")
            elif (player, computer) == ("Foot", 2):
                print("You LOSE!")
            elif (player, computer) == ("Foot", 3):
                wins += 1
                print("You WIN!")
            elif (player, computer) == ("Nuke", 2):
                print("both loose")
            elif (player, computer) == ("Nuke", 1):
                wins += 1
                print("You WIN!")
            elif (player, computer) == ("Nuke", 3):
                print("You LOSE!")
            elif (player, computer) == ("Cockroach", 1):
                print("You LOSE!")
            elif (player, computer) == ("Cockroach", 2):
                wins += 1
                print("You WIN!")
            elif (player, computer) == ("Cockroach", 3):
                draws += 1
                print("It is a tie!")
            
        
        
        
        
        

if __name__ == "__main__":
        wins = 0
        rounds = 0
        draws = 0
        main()
        
Reply
#8
(Dec-05-2016, 12:23 AM)Jei Wrote: how would you have done it?

if __name__ == "__main__":
        wins = 0
        rounds = 0
        draws = 0
        main()

Those 3 lines can be omitted, they follow inside main() anyway.
Reply
#9
You could also take out the sub function and the call to the sub function. They're not doing anything since you don't return anything from sub to the main function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
(Dec-05-2016, 02:47 PM)ichabod801 Wrote: They're not doing anything since you don't return anything

Sub is used for printing :)

@Jei: How do you handle wrong input by the user? Maybe they had a typo
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I attempted to make a rock paper scissors bot for homework Intellectual11 3 2,889 Jun-24-2021, 08:00 PM
Last Post: deanhystad
Question Rock, paper, scissors spelling error banidjamali 6 3,170 Jan-19-2021, 02:51 PM
Last Post: banidjamali
  Rock, Paper, Scissors Game kramon19 2 5,319 Jan-10-2020, 08:18 AM
Last Post: perfringo
  I need help with a python Rock Paper Scissors game urgently. Riff_Raff 3 5,821 Dec-05-2018, 09:13 PM
Last Post: nilamo
  Rock, Paper, Scissors Advanced that saves, loads, and keeps statistics EvanCahill 0 5,198 Jul-21-2018, 07:32 PM
Last Post: EvanCahill
  Rock Paper Scissors Warmlawpk441 4 5,013 Oct-11-2017, 10:55 AM
Last Post: gruntfutuk
  Rock paper scissors game samiraheen 3 6,340 Oct-03-2017, 07:07 PM
Last Post: buran
  The Python Book altered rock paper scissors Python_Noob 0 2,908 Sep-18-2017, 06:13 AM
Last Post: Python_Noob
  HELP---problems with rock paper scissors games kalt91 2 4,110 Sep-15-2017, 04:51 PM
Last Post: micseydel
  Rock, Paper, Scissors game help.. hentera 3 5,033 May-19-2017, 10:56 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020