Trouble with return - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Trouble with return (/thread-16512.html) |
Trouble with return - Rudy - Mar-02-2019 I am super to new to this. I'm working on an assignment that is a calculator that uses random method, defines a function, and uses return. This is not the full code. I've been boiling it down to its smallest parts trying to figure out what I'm doing wrong. This is the part I'm working on right now. If I replace return with print, everything works fine, including the full code. But using return is part of the assignment. What am I doing wrong? import random x = random.random()*10 y = random.random()*10 rx = format(x, '.2f') ry = format(y, '.2f') def numbers(x,y): z = rx + ry return z numbers(x,y)This looks exactly like half of the examples I've seen online but theirs work fine and I still can't get anything to return a value Thank you for helping! RE: Trouble with return - purpleicetwice - Mar-02-2019 That return value needs to be stored somewhere in order to print it. result = numbers(x,y) print(result) RE: Trouble with return - Larz60+ - Mar-03-2019 you can use: print(numbers(x,y)) RE: Trouble with return - Rudy - Mar-03-2019 Thank you! This helped a lot. I thought return was supposed to replace print, so I was getting really frustrated. I can't get this program working with return - Rudy - Mar-03-2019 Here's the full program. I've been doing all sorts of google searches trying to find the answer to this on my own but I'm missing something. This is a calculator that defines a function, uses random inputs, and uses break. import random op = input('Enter one of these operations: +, -, *, /, or q to quit: ') x = random.random()*10 y = random.random()*10 u = format(x, '.2f') v = format(y, '.2f') def numbers(u,v): print('numbers called with ', u, 'and ',v) if op == '+': add = u + v return add print(u,'+',v, '=' ,numbers(u,v)) elif op == '-': difference = u-v return difference print(u,'-',v, '=' ,numbers(u,v)) elif op == '*': product = u*v return print(u,'*',v, '=' ,numbers(u,v)) elif op == '/': quotient = u/v return quotient print(u,'/',v, '=' ,numbers(u,v)) elif op == 'q': print('Quitting...') else: op not in '+-*/q' print('Unknown') numbers(u,v)With the code like this, the only thing that prints is the top print line. I've tried playing around with indent levels and I can get a result from return by unindenting the print line associated with it, but then none of the following code works. The other problem I'm having is that it's not performing the math. For example, 1 + 2 = 12 I have spent hours working on this and I feel more than a little dumb. Any help is greatly appreciated. Thanks! RE: I can't get this program working with return - Yoriz - Mar-03-2019 You never printed the result of the function. Numbers are turned into strings. Also note when no return value is given it will print None, see various comments in code. import random op = input('Enter one of these operations: +, -, *, /, or q to quit: ') x = random.random()*10 y = random.random()*10 u = round(x, 2) # was returning string v = round(y, 2) # was returning string def numbers(u,v): print('numbers called with ', u, 'and ',v) if op == '+': add = u + v return add print(u,'+',v, '=' ,numbers(u,v)) # this wont print because it stops at the return elif op == '-': difference = u-v return difference print(u,'-',v, '=' ,numbers(u,v)) # this wont print because it stops at the return elif op == '*': product = u*v return product # you forgot to return product print(u,'*',v, '=' ,numbers(u,v)) # this wont print because it stops at the return elif op == '/': quotient = u/v return quotient print(u,'/',v, '=' ,numbers(u,v)) # this wont print because it stops at the return elif op == 'q': print('Quitting...') else: op not in '+-*/q' # This line is not doing anything print('Unknown') result = numbers(u,v) # added a variable to assign the result to print(result) # printed the returned result contained in the variable RE: Trouble with return - Rudy - Mar-03-2019 Thank you!! Things are working now. |