r/learnpython • u/Safe-Cockroach-9246 • 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.
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, ywhere 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) 102
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
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) American2
1
u/frnzprf Sep 19 '26
Yes.
When someone writes
print("Hello ", name), the comma doesn't mean combine strings. It means "usenameas the second argument ofTo 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")).
4
u/Rubix321 Sep 18 '26
Looks like you're returning a tuple instead of a string, so it's printing the tuple. As others have suggested, good look up fstrings, they're your friend!
2
u/Rubix321 Sep 18 '26
Alternatively, you want to join the two outputs in that return statement into one string
3
u/JGhostThing Sep 18 '26
Please format the text properly. This is especially true in python, because white space is syntactic.
2
u/codeguru42 Sep 19 '26
Pro tip: use triple backticks to format a block of code rather than single backticks for each line
1
u/ryuugami47 Sep 18 '26
That return line returns a tuple. The first element is a string and the second element is the value if average. That's happening, because you are using that "," in your return statement.
1
u/tbobsj Sep 18 '26
Your check function returns (sometimes, as in your example) a tuple. When you call print on a tuple, all parts of the tuple are changed to strings. If one component of the tuple is a string itself, it is printed as this string, namely without expanding escape sequences.
If you want to separate a tuple, you can use unpacking with the *-operator:
average = 11.5
returnvalue = ('Both are above 10. \n average is', average)
print(*returnvalue)
does print out your result, but will not work if returnvalue is just a float, which is the other type your function returns. You could check with isinstance(returnvalue, tuple) or you could check with type(returnvalue) is tuple before you print out the two different values. The better version is to rewrite your function so that it always returns the same type and then print out accordingly:
def check(a, b):
average = (a+b)/2
if a>10 and b>10:
return 'Both are above 10. \n average is', average
else:
return '', average #This is a kludge
print(*check(13, 15))
This results in the intended output. Please note that there is no reason to use a and b as parameters to the function and then ask the user to input the values for a and b.
Both are above 10.
average is 14.0
This is still a kludge, as print will insert a space between the empty string and the average converted to a string. If you want to avoid the initial space, you can test whether the first component of the tuple is the empty string or not.
What you should do (since you do not know a lot of Python yet) is to avoid tuples until you learn them.
1
1
u/CranberryDistinct941 Sep 18 '26
return "Both are above 10. \n average is", average
You're returning a tuple where the first element is a string and the second element is a number. Return statements work differently than print statements so if you want it to be a string you have to explicitly make it into a string: "Both are above 10. \n average is" + str(average)
1
u/cdcformatc Sep 18 '26
you are returning a tuple and printing it
that's what a tuple looks like when printed
if you want it formatted a certain way, you have to use some sort of string formattingÂ
14
u/Outside_Complaint755 Sep 18 '26
Sounds like your printing issue is handled, but just wanted to point out that on this line:
you are passing the input function itself as both arguments to your function. Ultimately this has no effect as you don't use either passed in value and then overwrite them with the results of new calls to
input(). Either the prompts for user input should happej before you call the function, or you should remove the parameters from the function definition.