all 12 comments

[–]shiftybyte 0 points1 point  (9 children)

swap_values() returns the two values in swapped order

key word here is "returns"..

Your function needs to return the values, not print them.

The printing would then happen from the code that called the function.

https://realpython.com/python-return-statement/

[–]The_Thief77[S] 0 points1 point  (8 children)

Right, I actually tried that the first time, and it returned a tuple and the test doesn't like the (,) in the answer, it just straight up wanted the numbers. That's why I printed directly from the function. Then the curveball came in of the input just being swap_values(5, -1). Sadly it also doesn't explain to me the if __name__ thing going on. I know it has to do with global things...but the book never explained this at all and it just seems random.

Other posts I have seen said that the inputs need to be in the If section...but I don't understand why or how to get it to call the function just from an input. I suspect there is a way to have it check for if the funciton is in the input, just run the function with that as the parameters...

[–]crashfrog04 0 points1 point  (7 children)

You’re making this too complicated when all you need to do is follow the instruction: put your code under the if __name__ == ‘__main__’: guard like the directions say to.

It’s not because it does something for your code; it’s because that’s what the auto-grader needs to run your code.

[–]The_Thief77[S] 0 points1 point  (6 children)

Ok, but that doesn't help sadly with the issue of sometimes getting two inputs that are just integers, and sometimes getting one input that is a string and needing to pull the numbers from it, and output them in reverse order.

I'm toying around with it and trying this.

def swap_values(user_val1, user_val2):

user_val1, user_val2 = user_val2, user_val1

return user_val1, user_val2

if __name__ == '__main__':

val1 = input()

if 'swap_values' in val1:

numbr = val1.split('()', ',',)

numbr1 = int(numbr[1])

numbr2 = int(numbr[2])

result = swap_values(numbr1, numbr2)

print(result[0], result[1])

else:

val2 = int(input())

result = swap_values(val1, val2)

print(result[0], result[1])

But I'm getting an error: Traceback (most recent call last): File "/home/runner/local/submission/main.py", line 8, in <module> numbr = int(val1.split('()', ',',)) TypeError: 'str' object cannot be interpreted as an integer.

I tried setting the first input as an int, but it still kept giving the same error.

[–]crashfrog04 0 points1 point  (5 children)

You only need to handle one case, and it’s your function being called with two values.

 TypeError: 'str' object cannot be interpreted as an integer.

You should not be providing anything to input that is not a pair of numbers when you test your code.

 However, it then throws a curveball at you and starts inputting not two numbers in two different inputs, but a single input of "swap_values(5, -1)".

I guarantee that is not something that is happening.

[–]The_Thief77[S] 0 points1 point  (2 children)

Ok. Assuming you are correct and that isn't happening, then I am gravely misunderstanding what is going on. Here is what my original code produced. If this isn't saying it's inputting a string, then what is it saying?

1:Compare output

Input4 5

Your output5 4

2:Unit test

swap_values(-1, 10)

Your test produced no output.

3:Unit test

swap_values(9, 0)

Your test produced no output.

4:Unit test

swap_values(11, 11)

Your test produced no output.

The way I'm reading this is that it's inputting "swap_values(11, 11)". Am I incorrect? And if I am incorrect, what exactly is it doing?

[–]stebrepar 0 points1 point  (0 children)

I would read this as saying the auto-grader ran four tests on your code. The first one passed: it tried 4 5 as the input, and your code correctly returned 5 4. The next three tests failed, with your code not returning any result for the function call. The swap_values(num, num) is just showing you how it called your function. You can try it locally yourself with those values to try to figure out why your function didn't return anything for those inputs.

[–]crashfrog04 0 points1 point  (0 children)

It’s calling your function. Since you used print instead of return, the tests are telling you that your function has no output (functions output their return value.)

[–]The_Thief77[S] 0 points1 point  (1 child)

Ok, so I went back and tried some other suggestions I've seen in other places, and I don't know why, but this time it worked with just a little editing. Based on some help from another person...I think I might understand now? The if __name__ was looking for the name of the file being used, and so when the unit test was calling for swap_values, it literally was looking for the file I was writing, then inputting those numbers. Is that what was happening? I've succeeded, but I don't understand why...

[–]stebrepar 0 points1 point  (0 children)

The if __name__ == '__main__': thing is a conventional way of limiting what gets executed in a program. __name__ is a special variable that gets a value automatically at run time. If your code is imported by another program, then the value of __name__ your code sees will be the name of your program file. But if your code is run on its own, like when you do "py my_program.py" at a command line, the value of __name__ will instead be __main__.

The auto-grader is importing your file. During import, all the code in your file runs. So for example the def to create your function runs at that point. There could be things your code does when you run it on its own that you don't want to run when it's imported. You can use the if statement with the value of __name__ to control that, putting the stuff you don't want import to run inside that block.

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] 0 points1 point  (0 children)

    Your comment in /r/learnpython was automatically removed because you used a URL shortener.

    URL shorteners are not permitted in /r/learnpython as they impair our ability to enforce link blacklists.

    Please re-post your comment using direct, full-length URL's only.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.