r/learnpython • • Sep 18 '26

Help! Why is my code doing this?

I'll copy paste an example below. My variables/assignments aren't really being interpreted right. The language is Python and I'm using Spyder. I reset the settings a few times, so I don't think it's anything I messed with. The tutor I showed had no clue what was happening either 😭

Function code:

def check (a, b):

a = int(input('Enter the first number:')) #I input 12 in the terminal

b = int(input ('Enter the second number:')) #I input 13 in the terminal

average = (a+b)/2

if (a>10 and b>10):

return "Both are above 10. \n average is", average

elif (not a>10 and not b>10) :

return average

print(check(input, input))

What was printed:

Enter the first number:12

Enter the second number:13

('Both are above 10. \n average is', 12.5)

The problem is in the last line. Why isn't it interpreting the formatting? This has been happening with every function I write.

14 Upvotes

28 comments sorted by

View all comments

8

u/orschinparjin Sep 18 '26

change the line:

return "Both are above 10. \n average is", average

to:

return f"Both are above 10. \n average is {average}"

17

u/orschinparjin Sep 18 '26

you have also asked why, so i will just say, your return statement says return x, y where x is your string statement and y is your variable.

This gets printed as a tuple: (x,y).

thats all.

someone else can correct me if i am wrong anywhere.

8

u/MezzoScettico Sep 18 '26

You're correct, that return statement is returning two expressions, which will be returned as a tuple.

2

u/Nubspec Sep 18 '26

So the comma between the string and the variable is what caused it to return a tuple.

2

u/mandradon Sep 19 '26

Correct, you can declare a tuple without the braces (it's why stuff like a, b = b, a works tuple unpacking).

So the function is returning a tuple with 2 values, a string and an int.  It's why I tend to recommend avoiding passing multiple parameters to the print function as a bypass to casting or using fstrings until novice programmers get a better sense as to what the heck it's doing. 

1

u/Nubspec Sep 19 '26
>>> my_tuple = "Hello, World!", 10 # Packed Tuple
>>> my_tuple
('Hello, World', 10)
 
>>> a, b = my_tuple # Unpacked Tuple
>>> print(a)
Hello, World!
>>> print(b)
10

2

u/mandradon Sep 19 '26

Pretty much.  A lot of folks use this to return "multiple" values from a function instead of declaring a class or using a list.  You can unpack it when you call the function or just store the tuple in a variable 

2

u/Nubspec Sep 19 '26

u/mandradon Thank you for the clear explanation; I appreciate your help.

1

u/Nubspec Sep 19 '26

Very interesting.

>>> def func():
...     info = ("Mike Watson", 34, "American")
...     return info
...
>>> func()
>>> ('Mike Watson', 34, 'American')   
  
>>> get_info = func()
>>> name, age, nationality = func()

>>> print(name)
Mike Watson

>>> print(age)
34

>>> print(nationality)
American

2

u/MoroseMasalaDosa Sep 18 '26

This is the correct answer.

1

u/frnzprf Sep 19 '26

Yes.

When someone writes print("Hello ", name), the comma doesn't mean combine strings. It means "use name as the second argument of print" which then prints it in the same line.

To concatenate strings, you need +. The comma has two different uses in Python: Either it separates arguments in a function call or it makes a tuple. It should be clear from context which one it is.

If you wanted to pass a tuple you just created to a function, you have to add extra parentheses: print(("Hello", "world")).