Python 3如何在函数中正确返回值以供以后使用

时间:2018-03-05 07:04:40

标签: python

我是一个蟒蛇新手。我创建了一个计算器程序,它将接受来自用户的2个数字和一种操作。我已经有了一个工作代码,但我希望通过探索和使用函数来进一步简化代码。 这是代码的一部分:

def addition(num1,num2):
    sum = num1 + num2
    print('The sum is ', sum)
def subtraction(num1,num2):
    sub = num1 - num2
    print('The difference is ', sub)
def inputNumber():
    num1 = float(input('Enter the first number: '))
    num2 = float(input('Enter the second number: '))
    return num1,num2
print('Enter the corresponding number to perform the operation:\n')
print('1 - addition')
print('2 - subtraction')
print('q - quit')
while True:
    try:
        operation = input('Select operation > ').lower()
    if operation == 'q':
        break
    elif operation == '1':
        addition(inputNumber())
    elif operation == '2':
        subtraction(inputNumber())
    else:
        print('Not valid. Try again.')
except:
    print('Invalid!')

我的问题是在输入2个数字后它没有执行操作。我认为问题是2个输入值没有正确返回。

由于

3 个答案:

答案 0 :(得分:0)

这应该有所帮助。 inputNumber函数返回你的两个浮点数的单个元组。

def addition(num):    #--->Update
    sum = num[0] + num[1]     #--->using index to get first and second number.
    print('The sum is ', sum)
def subtraction(num):    #--->Update
    sub = num[0] - num[1]      #--->using index to get first and second number.

    print('The difference is ', sub)
def inputNumber():
    num1 = float(input('Enter the first number: '))
    num2 = float(input('Enter the second number: '))
    return num1,num2                 #-----> Returns a tuple. EX: (3.0, 4.0)
print('Enter the corresponding number to perform the operation:\n')
print('1 - addition')
print('2 - subtraction')
print('q - quit')
while True:
    try:
        operation = input('Select operation > ').lower()
        if operation == 'q':
            break
        elif operation == '1':
            addition(inputNumber())
        elif operation == '2':
            subtraction(inputNumber())
        else:
            print('Not valid. Try again.')
    except Exception, e:
        print('Invalid!', e)

答案 1 :(得分:0)

def addition((num1,num2)):
    sum = num1 + num2
    print('The sum is ', sum)
def subtraction((num1,num2)):
    sub = num1 - num2
    print('The difference is ', sub)
def inputNumber():
    num1 = float(raw_input('Enter the first number: '))
    num2 = float(raw_input('Enter the second number: '))
    return num1,num2
print('Enter the corresponding number to perform the operation:\n')
print('1 - addition')
print('2 - subtraction')
print('q - quit')
while True:
    try:
        operation = raw_input('Select operation > ').lower()
        if operation == 'q':
            break
        elif operation == '1':
            import pdb;pdb.set_trace()
            addition(inputNumber())
        elif operation == '2':
            subtraction(inputNumber())
        else:
            print('Not valid. Try again.')
    except:
        print('Invalid!')
  1. 使用raw_input从包含stringint的用户那里获取输入(仅适用于python 2)
  2. 检查Try except缩进。

  3. return关键字在元组中返回,因此修改加法和减法。

答案 2 :(得分:0)

问题是你的函数需要两个输入,但是得到一个元组(当你使用return foo, bar时函数会返回)

你可以使用这样的astrix扩展元组(也修复了缩进问题):

def addition(num1,num2):
    sum = num1 + num2
    print('The sum is ', sum)
def subtraction(num1,num2):
    sub = num1 - num2
    print('The difference is ', sub)
def inputNumber():
    num1 = float(input('Enter the first number: '))
    num2 = float(input('Enter the second number: '))
    return num1,num2
print('Enter the corresponding number to perform the operation:\n')
print('1 - addition')
print('2 - subtraction')
print('q - quit')
while True:
    try:
        operation = input('Select operation > ').lower()
        if operation == 'q':
            break
        elif operation == '1':
            addition(*inputNumber())
        elif operation == '2':
            subtraction(*inputNumber())
        else:
            print('Not valid. Try again.')
    except:
        print('Invalid!')