Reputation: 51
def max_num(num1, num2, num3):
if num1 > num2 and num3:
return num1
elif num2 > num1 and num3:
return num2
elif num3 > num1 and num2:
return num3
print(max_num(-10, 0, 10)) # first check, returns 0 (wrong)
print(max_num(-10, 5, -30)) # second check, returns 5 (right)
print(max_num(-5, -10, -10)) # third check, returns -5 (right)
I'm only something like 3 days into learning my first programming language (Python), and I was given a task to compare 3 numbers and return the largest. The 2nd and 3rd print checks are correct, as 5 and -5 are the largest out of their 3 numbers. However, the first check returns 0, which is obviously not the largest number, 10 is.
Upvotes: 0
Views: 3976
Reputation: 1
a = int(input('Enter First Number:'))
b = int(input('Enter Second Number:'))
c = int(input('Enter Third Number:'))
max = 0
if a > max:
max = a
if b > max:
max = b
if c > max:
max = c
print (max)
Upvotes: -2
Reputation: 1
I am new to coding and This is how i did it
number_1= int(input("Enter first number"))
number_2= int(input("Enter second number"))
number_3= int(input("Enter third number"))
if number_1 > number_2 and number_1 > number_3:
print("the largest number", number_1)
elif number_2 > number_1 and number_2 > number_3:
print("The largest number is", number_1)
elif number_3 > number_1 and number_3 > number_2:
print("The largest number is",number_1)
Enter first number 70
Enter second number60
Enter third number50
the largest number 70
Upvotes: 0
Reputation: 76
Using the reserved word "and" in python in a condition, returns True only if both sides of it are True. An Integer is considered True if it's not zero. for Example:
num1 = 5
num2 = 1
num3 = 0
return num1 > num2 and num2 > num3 # returns True because all the conditions are true
return num1 > num2 and num1: # returns True because num1>num2 and num1 is not 0
return num1 > num2 and num3: # returns False because one of the conditions is False becuase num3 is 0
If so, the conditions you've sent are considered like:
if num1 > num2 and num3: # if num1 > num2 and num3 is not 0
elif num2 > num1 and num3: # if num2 > num1 and num3 is not 0
elif num3 > num1 and num2: # if num3 > num1 and num2 is not 0
Therefore, the first one returns 0 because in the last condition you're checking if num2 is not 0 but it is.
A simple way to solve it:
def max_num(num1, num2, num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num1 and num2 > num3:
return num2
return num3
print(max_num(-10, 0, 10)) # returns 10
print(max_num(-10, 5, -30)) # returns 5
print(max_num(-5, -10, -10)) # returns -5
Upvotes: 1
Reputation: 1115
Your logical statement is not executing in the way you are thinking. In num1 > num2 and num3
it will check whether num1
is greater than num2
and num
is 0
or not. you can change your conditons to num1 > num2 and num1 > num3
. Try the following:
def max_num(num1, num2, num3):
if num1 > num2 and num1 > num3:
return num1
elif num2 > num1 and num2 > num3:
return num2
elif num3 > num1 and num3 > num2:
return num3
print(max_num(-10, 0, 10)) # returns 10
print(max_num(-10, 5, -30)) # returns 5
print(max_num(-5, -10, -10)) # returns -5
or like this:
def max_num(num1, num2, num3):
if num1 > num2:
if num1 > num3:
return num1
else:
return num3
else:
if num2 > num3:
return num2
else:
return num3
print(max_num(-10, 0, 10)) # returns 10
print(max_num(-10, 5, -30)) # returns 5
print(max_num(-5, -10, -10)) # returns -5
Upvotes: 1
Reputation: 2048
It is due to the fact that you can't factorize like this your condition. The condition:
if num1 > num2 and num3:
...
really means "If num1
is greater than num2
and num3
is different from 0
". Indeed, Python tries to convert num3
into a boolean. If num3
is an empty list, an empty string, 0
or False
, then it is evaluated to False. Otherwise, it is evaluated to True
. What you really wanted to write is, for instance:
if num1 > num2 and num1 > num3:
...
Upvotes: 2
Reputation: 659
While this does answer your question, it does not fulfill the intent of your class.
max([1, 3, 2]) # 3
when using the and operator you should think of it as this.
if (condition) and (condition):
so use if num1 > num2 and num1 > num3:
Upvotes: 4