Python Forum
Learning Python, newbie question about strings and evaluation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning Python, newbie question about strings and evaluation
#11
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
Reply
#12
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
>>>
Reply
#13
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.
Reply
#14
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 691 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 675 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 594 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  UndefinedEnvironmentName: 'extra' does not exist in evaluation environment EarthAndMoon 3 1,680 Oct-09-2023, 05:38 PM
Last Post: snippsat
  Trying to understand strings and lists of strings Konstantin23 2 757 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 964 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  numpy newbie question bcwilly_ca 4 1,177 Feb-10-2023, 05:55 PM
Last Post: jefsummers
  Python newbie laleebee 2 1,316 May-24-2022, 01:39 PM
Last Post: laleebee
  Splitting strings in list of strings jesse68 3 1,757 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Conditional evaluation stsxbel 7 3,493 Jun-13-2021, 08:27 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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