Well im having a little problem with some tuples or more so about defining which tuple

#Start of my tuples
        Rock = ("Scissors","Fire","Snake","Human","Wolf","Sponge","Tree")
        Fire = ("Scissors","Paper","Snake","Human","Tree","Wolf","Sponge")
        Scissors = ("Air","Tree","Paper","Snake","Human","Wolf","Sponge")
        Snake = ("Human","Wolf", "Sponge", "Tree","Paper","Air","Water")
        Human = ("Tree","Wolf","Sponge","Paper","Air","Water","Dragon")
        Tree = ("Wolf","Dragon","Sponge","Paper","Air","Water","Devil")
        Wolf = ("Sponge","Paper","Air","Water","Dragon","Lightning","Devil")
        Sponge = ("Paper","Air","Water","Devil","Dragon","Gun","Lightning")
        Paper = ("Air","Rock", "Water", "Devil","Dragon","Gun","Lightning")
        Air = ("Fire","Rock","Water","Devil","Gun","Dragon","Lightning")
        Water = ("Devil","Dragon","Rock","Fire","Scissors","Gun","Lightning")
        Dragon = ("Devil","Lightning","Fire","Rock","Scissors","Gun","Snake")
        Devil = ("Rock","Fire","Scissors","Gun","Lightning","Snake","Human")
        Lightning = ("Gun","Scissors","Rock","Tree","Fire","Snake","Human")
        Gun = ("Rock","Tree","Fire","Scissors","Snake","Human","Wolf")

now after this i also ask the user for there choice with a simple
user = raw_input("What is your Choice?")
this works all sweet and such until i got to the if statements

now what i wanted to do was use the variable user to identify which tuple must be searched to see if the player has won

if comp in user

Comp is another variable which holds the computers choice (randomly generated) and user is obviously what the user put in (theres validation to check that it is an existing choice with another tuple) i want to somehow manipulate the string that user holds to identify the tuple this would result in 3 If statements (Win, Draw, Loss) opposed to 15 and i later want to expand this to more gestures like RPS-101 or something

any ideas?

Recommended Answers

All 4 Replies

You probably want a dictionary:

import random

all = {"rock":"scissors",
         "scissors":"paper",
        "paper":"rock"}

user = raw_input("What is your choice? ")
comp = random.choice(all.keys())
print "Computer's choice:",comp
if all[user] == comp:
   print "You win!"
elif all[comp] == user:
   print "Comp wins!"
else:
   print "tie!"

The reasoning here is this: you want to map the user and computer choices to the options that they beat. Almost every map is most easily represented by a dictionary.

Jeff

well a dictionary works well for simple RPS but this is RPS-15 with future hopes of reaching 101 with only 15 thats over 100 different dictionary conditions. With tuples its only 15.
Any other ideas?

Try if comp in locals()[locals()]:

>>> Rock = ("Scissors","Fire","Snake","Human","Wolf","Sponge","Tree")
>>> user = 'Rock'
>>> locals()['user']
'Rock'
>>> locals()[locals()['user']]
('Scissors', 'Fire', 'Snake', 'Human', 'Wolf', 'Sponge', 'Tree')
>>> comp = 'Sponge'
>>> if comp in locals()[locals()['user']]:
...     print "In there!"
... else:
...     print "Not there."
...
In there!
>>>

This is possibly not the cleanest of approaches, and I'm not sure Python guarantees locals() will always do what it does today, but if you can't come up with anything more elegant you might consider the above.

well a dictionary works well for simple RPS but this is RPS-15 with future hopes of reaching 101 with only 15 thats over 100 different dictionary conditions. With tuples its only 15.
Any other ideas?

I think I'm missing something. Whether with tuples or dictionaries, it seems like you would only need one entry per item. If, say, you have four items that can take out different things ...

Rock -- smashes scissors, hammer
Paper -- covers rock, wraps hammer
Hammer -- pummels scissors
Scissors -- cuts paper

Then we have

import random
all = {"rock": ("scissors","hammer"),
         "scissors": ("paper",),
         "paper": ("hammer","rock"),
         "hammer": ("scissors",)}

user = raw_input("What is your choice? ")
comp = random.choice(all.keys())
print "Computer's choice:",comp
if comp in all[user]:
    print "You win!"
elif user in all[comp]:
    print "Comp wins!"
else:
    print "tie!"

So unless I'm misunderstanding your plan, this oughta work.

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.