Python Forum
rock,paper,scissors - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: rock,paper,scissors (/thread-1102.html)

Pages: 1 2


rock,paper,scissors - Jei - Dec-04-2016

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?


RE: rock,paper,scissors - Yoriz - Dec-04-2016

Comparison is done by double ==, assignment is one =


RE: rock,paper,scissors - Jei - Dec-04-2016

(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?


RE: rock,paper,scissors - stranac - Dec-04-2016

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.


RE: rock,paper,scissors - ichabod801 - Dec-04-2016

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.


RE: rock,paper,scissors - Yoriz - Dec-04-2016

You could use
if (user1, user2) == ('rock', 'rock'):



RE: rock,paper,scissors - Jei - Dec-05-2016

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()
        



RE: rock,paper,scissors - Kebap - Dec-05-2016

(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.


RE: rock,paper,scissors - ichabod801 - Dec-05-2016

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.


RE: rock,paper,scissors - Kebap - Dec-05-2016

(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