There's a bunch of problems with your code:
get_side(‘a’,’b’,’c’)
Calling a function like that will work (assuming your editor changes it to get_side('a','b','c')
with regular quotes, as @silviomayolo correctly points out, you should use a proper text or code editor), since your function's signature is:
def get_side(a,b,c):
But it appears you really just want a, b, and c to be variables with the three sizes that have been entered. What your call does is assigning a
the value 'a'
, b
the value 'b'
, etc. You don't need those values in your function, so there's no point.
Also, your function isn't returning anything and what you're doing won't make the values available outside the function. You need a return
statement (or their evil cousin, a global
variable).
Your function also repeats itself three times, a more efficient solution would be to just have one function that requests a single value, and call it three times.
Finally, you also don't want 0 as a value, so you'll want to check for <= 0
.
So you were probably after something like this:
def get_side(n):
side = int(input(f'Enter Side Length #{n}: '))
while side <= 0:
side = int(input(f'Try Again! Enter Side Length #{n}: '))
return side
a = get_side(1)
b = get_side(2)
c = get_side(3)
Note that a string like f'number {n}'
automatically replaces the {n}
part with the value of n
when it is evaluated.
n
has the right value every time, because get_side()
is called with a number, and n
will have that value every time the function runs. So, the function will print the correct message for the three numbers.
However, those three last lines still look very repetitive, you could also:
a, b, c = (get_side(n) for n in range(1, 4))
range(1, 4)
will loop from 1 up to (but not including) 4, and in the expression above, get_side(n)
will be called with those values, so you get three values, which Python can assign to a
, b
, and c
. In one go.
So the solution would be:
def get_side(n):
side = int(input(f'Enter Side Length #{n}: '))
while side <= 0:
side = int(input(f'Try Again! Enter Side Length #{n}: '))
return side
a, b, c = (get_side(n) for n in range(1, 4))
print(f'User entered {a}, {b} and {c}')
And the user interaction could look something like:
Enter Side Length #1: 2
Enter Side Length #2: -1
Try Again! Enter Side Length #2: 0
Try Again! Enter Side Length #2: 4
Enter Side Length #3: 8
User entered 2, 4 and 8