Accepting multiple answers as an 'if' variable [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
How do I do a case-insensitive string comparison?
(15 answers)
Closed 5 years ago.
How would I accept multiple options/answers for an input. For example:
if player_ship == 'Transport' or 'TRANSPORT' or 'transport':
print('You successfully purchased a Transport Ship!\n\n')

The below will ensure that case sensitivity is not a problem.
if player_ship.lower() == 'transport'
If you genuinely need to check against specific values, this will work:
if player_ship in {'Transport', 'TRANSPORT', 'transport'}

Related

Is there a way to enter an if statement for .isdecimal? [duplicate]

This question already has answers here:
How can I check if a string represents an int, without using try/except?
(23 answers)
What's the difference between str.isdigit(), isnumeric() and isdecimal() in Python?
(4 answers)
Closed last year.
I need to check if a number is a whole or a decimal and input it into an if statement
ex.
if string_name.isdecimal() = true:
print('warning')
I'm trying to check for any overflow.

Python: one variable but different values? [duplicate]

This question already has answers here:
Why does adding a trailing comma after an expression create a tuple?
(6 answers)
What does __all__ mean in Python?
(10 answers)
Closed 5 years ago.
I have a line of code from Python forbidden fruit module:
__all__ = 'curse', 'curses', 'reverse'
I know what strings are and I know what arrays and tuples are. What kind of variable is this? How can we use this and for what?
It's a tuple. If you want to find out the type of something, use the type function - e.g.
type(__all__)

Can someone explain why print (2 and 3) just prints "3"? [duplicate]

This question already has answers here:
Does Python support short-circuiting?
(3 answers)
How do "and" and "or" act with non-boolean values?
(8 answers)
What's the value of short-circuit of Python?
(4 answers)
Closed 5 years ago.
This is a common question I've seen, but really don't get the programming logic behind it. I'm sure it's a simple solution, but one that I have yet to understand.

While loop only working for one condition [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
How do "and" and "or" act with non-boolean values?
(8 answers)
Closed 5 years ago.
Im trying to validate the user's input so he/she only types in 'all' or 'sel' (not case sensitive). Might just be some syntax mistake im making.
choice=input('Type "all" or "sel": ')
choice=choice.lower()
while choice!=('sel' or 'all'):
choice=input('Invalid input. Try again: ')
choice=choice.lower()
This only works if I type in 'sel', but not 'all'.

Replacement of strings in a column excluding some words in python [duplicate]

This question already has answers here:
How to filter Pandas dataframe using 'in' and 'not in' like in SQL
(11 answers)
Selecting with complex criteria from pandas.DataFrame
(5 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
I have been trying to change the words in one of the columns of my data frame such that it will replace all the words except some keywords which I want to leave as it is.
I tried doing this:
df['col]=df['col].where(lambda x: x == ('A'or'B'or'C'),'others')
My plan is to replace every word that isn't A or B or C with others.
The code worked for A but B and C were replaced.
How do I go about this please?
NB: I am trying to improve my python skill and have searched the entire stack-overflow before asking this question.
Thank you for your help.

Categories

Resources