Python Forum

Full Version: Learning Python, newbie question about strings and evaluation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
with respect I'm not sure you've understood. Perhaps I wasn't clear in my explanation.

If I could set "rock" > "scissors" and it meaningfully be True (in the sense that I'd intended) I wouldn't have a problem. But since I cannot, I have been trying to understand why - basically to discover what's happening with string evaluation. I didn't consider this to be nonsense. You're not obliged to answer of course.

But:

def assign(rock, paper, scissors):
    "rock" > "scissors"
    "scissors" > "paper"
    "paper" > "rock"
    return = True

print("rock" > "scissors")
gives me False

Why would I want this to be True? Because as I stated in my original post, I was exploring ways of coding the problem that might be neater or more efficient than simply writing a lot of if "this thing" == "that thing": #do something or other. Were it a problem using integers I'd be fine, but this is a word game.

I'm learning Python, it seemed entirely reasonable to ask for clarification. I didn't consider it nonsense, I was trying to find out why I could not compare strings in a certain way. Anyway, thank you for you help because it still helped me clarify some ideas/thinking Smile
Well I think I see what you are attempting.
That still can be done, but not like this.
What you need to do is equate each string to a number, and then compare the numbers.
one way can be done is:
>>> rock = 3
>>> sissors = 2
>>> paper = 1
>>> print(rock > sissors)
True
>>> print(sissors > paper)
True
>>> print(paper > rock)
False
>>>
The rules of the game are that:

rock beats scissors
scissors beats paper
paper beats rock
similar items tie

One of my first ideas was to assign an integer value to each string, but it was unclear to me how I could number them such that paper still beats rock. This is why I went with processing the strings.
OK try this on for size
rock = {
    'paper': False,
    'sissors': True,
    'rock': 'Tie'
}

paper = {
    'rock': True,
    'sissors': False,
    'paper': 'Tie'
}

sissors = {
    'paper': True,
    'rock': False,
    'sissors': 'Tie'
}

print('rock > paper: {}'.format(rock['paper']))
print('rock > sissors: {}'.format(rock['sissors']))
print('paper > sissors: {}'.format(paper['sissors']))
print('paper > rock: {}'.format(paper['rock']))
print('rock > rock: {}'.format(rock['rock']))
# etc.
results:
Output:
rock > paper: False rock > sissors: True paper > sissors: False paper > rock: True rock > rock: Tie
Pages: 1 2