Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with return
#1
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!
Reply
#2
That return value needs to be stored somewhere in order to print it.

result = numbers(x,y)
print(result)
Reply
#3
you can use:
print(numbers(x,y))
Reply
#4
Thank you! This helped a lot. I thought return was supposed to replace print, so I was getting really frustrated.
Reply
#5
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!
Reply
#6
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
Reply
#7
Thank you!! Things are working now.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020