Calculating a price with a if/else statement [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
the function i have written....
def calculate_price(Thickness, Width, Price, Timbmet):
foo = Thickness * Width;
bar = foo * Price;
if Timbmet == "n" or "N":
foobar = ((bar / 100) * 15) + 100
print foobar
else:
print bar
calculate_price(5., 4., 3., "y");
the console output is 109 which is wrong because it shouldn't have executed the code in the if statement because the Timbmet parameter does not match "n" or "N". What am I doing wrong and why is the code not running the else part?

Change condition to -
if Timbet in ["n", "N"]:
Programming does not work like natural language...

The condition Timbmet == "n" or "N" returns always True because the second subexpression "N" is always True. You need to either check if Timbet is in a list/set:
if Timbet in ("n", "N"):
if Timbet in {"n", "N"}: # even better but more confusing if we are not aware of the set construction syntax
... or check without the case sensitivity:
if Timbet.lower() == "n":

Related

Looking for an explanation of why this block does not work [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 14 days ago.
I am just starting to learn to code and I am experimenting with all sorts of methods. Might be a dumb question but why does this block only return the 0 value regardless of what I input?
start = input("Would you like to play? (Y/N)\n")
if start == "N" or "n":
game_state = 0
elif start == "Y" or "y":
game_state = 1
I have tried to change it in all sort of ways but I can t seem to make it work.
You need to change your if statements to:
if start == "N" or start == "n":
or to:
if start.lower() == "n":
A non-empty string will evaluate to True, so the first if statement will always run.

Why is my "or" statement not working as I want? [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
How to check variable against 2 possible values?
(6 answers)
Closed 7 months ago.
import time
repeat=0
price=0
print("Welcome to McDonald's self order system")
time.sleep(0.5)
name_person=str(input("\nPlease enter your name to continue: "))
while True:
order=int(input("\n---Menu---\n 1.Burger\n 2.McPuff\n 3.Ice Cream\n 4.Cold Drink\n\nPlease order by typing the number: "))
if order in range(1,4) :
repeat=str(input("Do you want to order more? Yes/No: "))
if repeat == "No" or "no":
print("Ok")
break
else :
print("\n!!Invalid input!!")
time.sleep(0.5)
The or command is not working when I am typing No it shows Ok that's normal but if I type Yes it should loop but then also it's showing Ok and when I am typing anything it's giving the output Ok but if I am removing the or its working perfectly.
Instead of if repeat == "No" or "no", use:
if repeat == "No" or repeat == "no"
Or even:
if repeat in ("No", "no")

How would I not take invalid inputs with this [duplicate]

This question already has answers here:
Comparing a string to multiple items in Python [duplicate]
(3 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 2 years ago.
Hi I'm new to python and I'm having some trouble with logical operators. In the code I want the user to input one of three choices A, S , or D and reject anything else. The problem Im having is that when I input A, S, or D it still prints out Invalid Input.
guess = input("Lower (a), Same (s), Higher (d): ")
if guess != "a" or "s" or "d":
print ("Invalid Input")
Im using python version 2.7 if that helps
Here or short circuit operator compares the two True values + the condition guess != 'a' (Because non empty values evaluate to True in Python), so use not in:
guess = input("Lower (a), Same (s), Higher (d): ")
if guess not in ['a','s','d']:
print("Invalid Input")

one-liner-return with 3 if-statements [duplicate]

This question already has answers here:
Putting an if-elif-else statement on one line?
(14 answers)
Closed 2 years ago.
I have the following Code in Python:
def show_sequence(n):
if n > 0:
return "+".join((str(i) for i in range(n+1))) + " = %d" %(sum(range(n+1)))
elif n == 0:
return "0=0"
else:
return str(n) + "<0"
Question: Is there a syntax correct way of putting all lines into one return statement if there are 3 if-statements?
I know it works with one if- & else-statement but im a fan of one-liner and already asked this myself several times.
Inline if-statements can be chained like this:
"a" if 0 else "b" if 0 else "c"
(replace the 0s with 1s to see the return value change)
You can use this:
result = True if a==b else False

Python conditional statement problem (logic) please go through body for problem description [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 4 years ago.
def gender_checker(sex="unknown"):
if sex=="m":
print ("male"):
elif sex=="f":
print ("female")
elif sex is not "m" or "f":
print (" invalid")
""" It's python program..(3.x.xx )
Problem is when no arguments is being passed it should print "unknown" buts its not printed that instead "invalid" is getting printed. What I want is if no args are passed then print default if args passed is in valid then print invalid that its """
Python isn't fully like English writing, so you would have to do:
def gender_checker(sex="unknown"):
if sex=="m":
print ("male"):
elif sex=="f":
print ("female")
else:
print ("unknown")
I think you are misunderstanding your if-else statement. It will print invalid since your last elif statement is checking if it is not m or f; I would imagine you want something like this:
def gender_checker(sex="unknown"):
if sex=="m":
print("male"):
elif sex=="f":
print("female")
elif sex == "unknown":
print("unknown")
else:
print("invalid")
The last condition should be:
elif sex != "m" or sex != "f":
But even simpler:
else:
Because this will cover all other cases where sex is neither "m" nor "f".
In addition remove the : behind print("male"):
and let it print after the last (else:) clause: print("unknown")

Categories