Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
+ 1

I'm working in this question and I'm not sure why we would return to a variable name. at the end is return answer; it's not making sense how that works.

25th Mar 2018, 6:04 PM
john whitehouse
3 Antworten
+ 1
To further explain Cals explanation.. def add(x,y): return x+y simple function that takes in 2 numbers and adds them together. how you use the return is up to you. maybe you want to get the sum of 2 random numbers for an addition program. num1=random.randint(1,9) num2=random.randiny(1,9) sum=add(num1,num2) print(num1) print("+") print(num2) answer=int(input(("Enter your answer - ")) if answer==sum: print("You are correct!") else: print("Wrong!")
25th Mar 2018, 6:49 PM
LordHill
+ 1
I think I understand it now it's return 0 for main function that's an int and it just needs it's value so return answer is returning the result of the function in the form of a variable
25th Mar 2018, 6:56 PM
john whitehouse
0
Say you have a function, that adds two numbers. You don't know whether the result will be printed to the screen, written to a file, display in graphics, or used in another calculation. You could write a function for each of these, or you could return the value to be used as it is needed.
25th Mar 2018, 6:25 PM
Cailyn Baksh