Advanced Python Code Challenges: Control Flow Challenge 5

Hi,
The question for the last challenge was this:
Create a function called max_num() that has three parameters named num1 , num2 , and num3 .

The function should return the largest of these three numbers. If any of two numbers tie as the largest, you should return "It's a tie!" .

So when I went to answer it I made this:

def max_num(num1,num2,num3):
  num1 = a
  num2 = b
  num3 = c
  if a > b and a > c:
    return a
  elif b > a and b > c:
    return b
  elif c > a and c > b:
    return c
  else:
    return "It's a tie!"

Which was fine when run alone, but after being run along with this:

print(max_num(-10, 0, 10))

received the error message:

Traceback (most recent call last):
File “script.py”, line 15, in
print(max_num(-10, 0, 10))
File “script.py”, line 3, in max_num
num1 = a
NameError: name ‘a’ is not defined

After receiving the error, I fiddled around with the code for a

and ended up with a successful solution in this:

def max_num(num1,num2,num3):
  a = num1
  b = num2
  c = num3

  if a > b and a > c:
    return a
  elif b > a and b > c:
    return b
  elif c > a and c > b:
    return c
  else:
    return "It's a tie!"

So my question is, how come the second piece of code was successful but the first failed?

1 Like

In the first piece of code, where did you assign values to a, b, and, c?

You seem to be saying that you didn’t call your function, so by fine you mean… didn’t run it.

@ausipciousnotation,

The error says it all:
image

You never defined the variables a, b, or c in the first code…

Thank you for your reply! I was just wondering, how come the 2nd code worked whereas the first didn’t? does it matter which side of the equal sign the variable you want to define is?

I didn’t run the actual function no, I just defined it and ran the definition on its own. Though it was run later on when the print statement occurred.

you’re probably going to need to run something to get it to crash.

Thank you, I will keep that in mind when testing in future. Do you perhaps know why the 2nd code worked whereas the first did not? Does it matter which side of the equal sign you place a variable when assigning a value?

@ausipciousnotation

The first code sets num1 = a, num2 = b, and num3 = c.
-10 = a
0 = b
10 = c
That means that when going through the loop, you would be doing:
if num1 > num2 and num1 > num3:
return num1
and so and so forth.
You never defined the variables a,b, and c.
You would have never been comparing a, b, and c to properly go through the loop.

The second code makes whetever numbers you have as the paramaters for num1, num2, and num3, (-10,0,10) and saves them to a, b, and c.
a = num1 (-10)
b = num2 (0)
c = num3 (10)
^ This is defining the variables a, b, and c.

if a(-10) > b(0) and a(-10) > c(10):
return a(-10)
elif b(0) > a(-10) and b(0) > c(10):
return b(0)
elif c(10) > a(-10) and c(10) > b(0):
return c(10)
else:
return “It’s a tie!”

In this case, it should return 10.

num2 = b

which variable would that change? why? why not the other way around?

because you are saying to set one to the other aren’t you? which is which? or are you saying something else?

1 Like

I thought I was doing that when I did this :

 num1 = a
  num2 = b
  num3 = c

though it seems to have failed when run

Well since num 2 would have had a value in whatever the user set ( in this case 0), I would have thought that the user would know the value of b through the value of num2
since 0 = b , b = 0.
The post is about how come b = 0 can’t be the other way around.

if you have

var1 = var2

then which one would give way? one has to forget its value. which?

according to your scheme of how = would work, that is.

var1 = 3
var2 = 4
var1 = var2
print(var1)  # result?
print(var2)  # result?

and this would have to have the same result, right?

var1 = 3
var2 = 4
var2 = var1
print(var1)  # result?
print(var2)  # result?
1 Like

Since b in the example didn’t have any value, I would have thought that it would simply have taken the value of num2.

In accordance with your example, it would be like not assigning a value to var2

var1 = 3
var1 = var2
print(var1)  # 3
print(var2)  # 3

In answer to your actual examples I would think that:

1.)

var1 = 3
var2 = 4
var1 = var2
print(var1)  # 4
print(var2)  # 4
var1 = 3
var2 = 4
var2 = var1  # so this would be illegal since it's not true?

why 4 and not 3? what determines that? why would 4 win over 3?

1 Like

I would think that the 2nd value changes the value of the 1st no? Since it is the value you want the first to now “equal”

AHHHH!
So in my code, since a, b and c have no value, they weren’t defined and therefore wouldn’t be able to change the value of the first?

not if

a = b

is equivalent to

b = a

if they’re equivalent, then your only option is to say that it’s illegal to have differing values in that operation

so if this is NOT allowed:

var1 = 3
var2 = 4
var1 = var2

then how would you update an existing variable?

you could actually say that variables may never be changed, there are languages that do this. python isn’t one of them

1 Like

So the 1st value is always changed unless it is included in an equation like

`(a = b) = (b = a)’ ?

Just want to make sure I’ve understood the explanation correctly

@ausipciousnotation,

No offense, but I’m a little confused how you made it all the way to “Advanced Python Code Challenges: Control Flow Challenge 5” without understanding how the = syntax works in Python.

If you are still confused, I would highly recommend reviewing syntax in Lesson 1:
https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-syntax/exercises/variables

There exists a statement, assignment statement.
It promises to do something when you use it.
It promises to set a variable to a value.
The syntax of that statement is:
VARIABLE = VALUE

Given that such a statement exists, you can leverage it to update what a variable refers to.

Same with all other operations. They promise something specific. What? You can leverage that.


The alternative behaviour you’re suggesting has two problems.
One, it’s ambiguous about what is assigned to what, or alternatively, does not allow re-assigning existing variables.
Two, you have no promise that you can point at that says this is what it does.

1 Like