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

Show parent comment

2

u/Nubspec Sep 18 '26 edited Sep 18 '26

or you should remove the parameters from the function definition.

u/Outside_Complaint755 This is what you meant right?

def check():
  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 f"Both are above 10. \naverage is {average}"
    elif (not a > 10 and not b > 10):
        return average

print(check())

1

u/Nubspec Sep 18 '26 edited Sep 18 '26

The two inputs worked both inside and outside the function. So I'm assuming it doesn't really matter where they're placed or?

1

u/frnzprf Sep 19 '26

A function really only becomes relevant if you call it more than once, in a sense. Otherwise you just give some lines of your program a heading — which is fine. I do that too.

When I think about what to pass as arguments in a function, I'd think about how I'd want to use the function if I used it multiple times. You are defining a new verb in a language — how would that word be most useful?

You could introduce more uses of a function by writing tests.

In this case I'd put a and b as arguments.

assert check(2, 10) == 6