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
  Waiting for input from serial port, then move on KenHorse 3 1,139 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  Help with to check an Input list data with a data read from an external source sacharyya 3 433 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  difference between forms of input a list to function akbarza 6 1,093 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  Retrieve word from string knob 4 516 Jan-22-2024, 06:40 PM
Last Post: Pedroski55
  pyaudio seems to randomly halt input. elpidiovaldez5 2 411 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Receive Input on Same Line? johnywhy 8 754 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  How to create a table with different sizes of columns in MS word pepe 8 1,635 Dec-08-2023, 07:31 PM
Last Post: Pedroski55
  manually input data jpatierno 0 357 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  extract substring from a string before a word !! evilcode1 3 566 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Using string input for boolean tronic72 3 727 Nov-01-2023, 07:48 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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