Python Forum
input ==("word vs input == "word
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
input ==("word vs input == "word
#1
Take note of line 25 and line 34.

when this code is ran, the first block of code (line 25 is in this block) requires parenthesis around the multiple choices.

when lower down in my code (line 34) it requires no parenthesis in order for it to accept the words that are allowed.

im using python 3x, and pycharm.

why does the top part require parenthesis in order for it to choose the exact words that im allowed (guy, GUY, boy, bo, b).
and the bottom part require NO parenthesis to choose the same words, pchoice == 'left' or 'LEFT' or 'L' or 'l" or 'Left':

shouldnt it be one or the other? why would the first part of code need parenthesis, and not the second part?


class Player:
    def __init__(self,name,age,gender):
        self.name = name
        self.age = age
        self.gender = gender

    def getInfo(self):
        print("Player Info")
        print(self.name)
        print(self.age)
        print(self.gender)

    def getHealth(self):
        return self.health

    def eatApple(self):
        self.health = self.health + 15
        return self.health


ur_name = input("What is your name? ")
print("Hi, {}.".format(ur_name))
ur_age = input("How old are you? ")
ur_gender = input("guy or girl?")
if ur_gender == ('guy' or 'GUY' or 'boy' or 'bo' or 'b'):
    print("alright, dude")
else:
    print("k, girl")
    p1 = Player(ur_name,ur_age,ur_gender)
    p1.getInfo()
    p1.health = random.randint(80,100)
    p1.getHealth()
    pchoice = input("Looks like we can go right of left, where should we go? ")
    if pchoice == 'left' or 'LEFT' or 'L' or 'L' or 'Left':
        print("left")
    else:
         print("we go right")
Reply
#2
In Python, and and or operations on non-bolean values behave in a very specific way - the evaluated value will get the last value that will make the statement equivalent to booleanTrue, but the result is not necessarily boolean

'a' or 'b' == 'a'
'a' and 'b' == 'b'
Without partenthesis, line 25 essentially boils down to
(url_gender == 'guy') or 'GUY'
- that is where it shortcircuits - making it either True or 'GUY'

The proper condition is
url_gender in ('guy', 'GUY', ..)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Quote:if ur_gender == ('guy' or 'GUY' or 'boy' or 'bo' or 'b'):
This has nothing to do with parenthesis. So many noobies stumble here that we even made a tutorial just for this problem. The problem lies in this. The parenthesis only exist as a container in which to hold the numerous content in the tutorial. but you could use any other sequence.

Quote:if pchoice == 'left' or 'LEFT' or 'L' or 'L' or 'Left':
In this case you dont even need to check the the whole string (even though you did it wrong) you can just check the first character
This accounts for all of them. 
if pchoice.lower()[0] == 'l'
Recommended Tutorials:
Reply
#4
(Apr-15-2017, 10:49 PM)metulburr Wrote: This accounts for all of them. 
if pchoice.lower()[0] == 'l'
In OP's case, there are also couple variants for a word 'boy'

Ooops, missed second part of the question
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
oh, i get it! thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  interactive process for input and output maiya 1 500 Mar-27-2025, 08:40 AM
Last Post: Gribouillis
  Βad Input on line 12 Azdaghost 4 920 Mar-25-2025, 02:40 PM
Last Post: deanhystad
  How to remove unwanted images and tables from a Word file using Python? rownong 2 718 Feb-04-2025, 08:30 AM
Last Post: Pedroski55
  Word matching with specific parameters CascadeDiver 3 854 Jan-28-2025, 02:10 PM
Last Post: perfringo
  How to revert back to a previous line from user input Sharkenn64u 2 820 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  How to make it so whatever I input into a script gets outputted on a different file spermatozwario 4 1,106 Nov-24-2024, 12:58 PM
Last Post: deanhystad
Question [SOLVED] Same input different output antarling 2 850 Oct-25-2024, 11:28 PM
Last Post: antarling
  word guessing game, hints STUdevil 1 1,520 Oct-12-2024, 01:53 AM
Last Post: menator01
  word game, how to give hints STUdevil 5 1,519 Oct-07-2024, 06:20 PM
Last Post: STUdevil
  I think I need to delete input data because returning to start fails thelad 2 1,060 Sep-24-2024, 10:12 AM
Last Post: thelad

Forum Jump:

User Panel Messages

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