More efficient comparison [duplicate] - python

This question already has answers here:
How to get max() to return variable names instead of values in Python?
(5 answers)
Most efficient way of making an if-elif-elif-else statement when the else is done the most?
(9 answers)
How can you print a variable name in python? [duplicate]
(8 answers)
Closed 15 days ago.
I'm finding the largest number from a set of numbers (4 in my case). Is there a more efficient method of running the the code below? I tried using 'match' however it seemed to be on the same level as using if's and elif's.
maximum = max(a,b,c,d)
if maximum == a:
print("a is max")
elif maximum == b:
print("b is max")
elif maximum == c:
print("c is max")
elif maximum == d:
print("d is max")

Related

i can't sum of this two number I don't know what's the problem [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 21 days ago.
a=input("a value is:")
b=input("b value is:")
sum =a+b
print("the Sum of {0} and {1} is {2}" .format(a,b,sum))
result is coming like this
a value is:1
b value is:3
the Sum of 1 and 3 is 13
when using input the input is entered as a string, just cast it to int before summing aka :
sum = int(a) + int(b)
Hope i helped :)

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 code doesn't work as expected? [duplicate]

This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Is it better to use "is" or "==" for number comparison in Python? [duplicate]
(6 answers)
Closed 4 years ago.
The expected behaviour for this is to print 2949 is the magic number! when it hits 2949, yet the loop doesn't. The loop seems to start at 19, then jump to 513920 - why is this?
magicNumber = 2949
for x in range(0, 100000000):
if (x is magicNumber):
print(x, "is the magic number!")

Python passing arguments from console [duplicate]

This question already has answers here:
python: while loop not checking the condition [closed]
(3 answers)
Closed 8 years ago.
In the following code:
def foo(n):
print "n value before if:",n #displays given num
if n <= 2:
print "n value:",n #not displayed even n is less than 2
num = raw_input()
print foo(num)
The if statement does not execute on giving inputs less than 2 for num.
So, why is if statement not executing?
raw_input returns a string, you are then comparing it to an integer.
Try converting it to an int:
num = int(raw_input())

Categories