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
Hi all,

I'm going through an online course to learn Python3. Am getting on fine but I seem interested in finding not simply a "correct" answer that the site will parse and accept, I seem interested in thinking about the most efficient ways to solve any particular problem.

My current task is to code a simple game of "rock, paper, scissors". I'd like to achieve this by, in the second part of my code, taking inputs from both players, store them as variables (input1, input2), and then use an if: test to find the winner. Seems sound in principle but what I do not know and do not know if it's possible is the following:

It seems that if I write "Rock > "Scissors" ... "Paper" > "Rock" and so on, to have the If: f statement evaluate each input and calculate the winner correctly in fact Python simply returns boolean values for these statements irrespective of my declarations. Is there a simple method of declaring these strings in some way that I can then state e.g. "This" > "That", or is this simply not possible? I realise there are other ways to solve the problem, but curious about whether the above approach could be made to work.
Of course you can do that, but the redirection is going to cost processing time.
Best to use meaningful names alone.
for example:
def check_gt(this, that):
    return this > that:

result = check_gt(rock, scissors)
"The poster doth profess too much, methinks."
but ... shows you're thinking!
Aha! Okay. Many thanks - I think I understand... if I have, I tried the following:

def assign_order(str1, str2, str3):
    return str3 > str2 > str1

result = assign_order("c", "b", "a")

print(result)
and which prints false

I'd like to understand what's going on here. Am I correct in thinking the function is designed to accept three inputs - those could be any type, correct? But by defining result to use strings something else then happens? Why otherwise would it return "false"?

Can I essentially force a string to be "greater than" another string by using a function (or any other means?).
which gives an answer only for c > b > a
The function will return what you tell it to return:
def assign_order(str1, str2, str3):
    return False
will accept 3 attributes but always return false
so please bear with me - I am a beginner Confused

It seems then that there is no way (why should I be surprised haha) that I can 'insist' that black is white for the purposes of the function or the individual script. Correct?

What I'm saying is that I now understand that return x > y simply evaluates the expression and gives me a True/False. It doesn't 'force' x to be greater than y, it simply examines its truth?

Perhaps my aim for the game was illogical and unachievable insofar as I wasn't asking it to evaluate whether rock > paper as its internal value but was trying to 'insist' that paper > rock - if you'll excuse the semantics this was to say:

"Paper is greater than rock" rather than

"is paper greater than rock: answer no"

Have I understood this correctly? If so, it means I cannot do what I hoped to because it's illogical or contrary?
It seems then that there is no way (why should I be surprised haha) that I can 'insist' that black is white
Not true.
def get color():
    # color = ... do color fetching here ...
    if color == 'black':
        color = 'white'
    return color
Still trying to fully understand this. Perhaps I'm still mistaken, so please bear with me.

From your above example, let's just assume that my conditions for winning are such that white ALWAYS beats black. And let's say that I automate my program so that my computer opponent can enter one of any ten random words.

def computer plays():
    # computer does some stuff and gives me a random word from a list...
    if comp_colour != "white": #it could be a tie
        comp_colour = "black"
    return colour
Perhaps it is my evaluation that is at fault, as perhaps if my_colour > comp_colour: is the wrong way to check? Even if I amend this part, what I want to know is - what is being checked or evaluated when comparing two strings in this case? If I decide big = small am I confusing myself through natural language - meaning I should not be looking at the words themselves, if that makes any sense.
first, you need to pass comp_colour as an argument:
def computer_plays(comp_colour):
Now your if statement is true if comp_colour is 'black) # true if not = 'white'
for is comp_colour == 'black' it's != 'white' so change 'black' to 'black'
You're getting your logic messed up.
I think what you're saying you want is:
if comp_colour == "white": #it could be a tie
    comp_colour = "black"
yes, that are correct, my logic was confused. However I've come to the conclusion that it's not possible to do this at all in the way I'd assumed. I'm not saying I'm right in this - after all, this would seem a classic rookie error "It can't be done!" - but I'll say why just in case there's more I can learn.

I reduced my central problem to trying to do this - which produces an error of course, because presumably this is not allowed:

def reassign(x, y):
    return x > y = True

reassign(5, 6) #my intention above is to produce the result 5 > 6 == True so that e.g. "rock" beats "scissors"

print(reassign(5, 6))
This is where the idea currently fails. I'm trying to assign some 'order of truth' to "rock", "paper" and "scissors". As far as I'm currently aware making a comparison does not allow me to do this. Of course I can define a function which takes

pn = input("Player n choice: ")
and compare them, but then I end up putting the same 'long hand' if/elif inside the function that are my current solution where no function is involved at all.
Of course it fails, and why do you keep saying it can't be done when it can.
you can't say:
return x > y = True
it would have to be
return True
I don't know why you would do that, what would be the purpose of calling it,
just set whatever you would use the return vale for to the value you want without calling
a function.

Enough on this foolishness, there are other posts to answer
Pages: 1 2