为什么我不能将每个数字拆分为单独的行?

问题描述 投票:0回答:1

对于像这样调用的 Python 任务:

编写一个程序,读取三个整数并在屏幕上显示它们的和和乘法。 每个数字都写在单独的行上。

我是这样写的

def sum(a, b, c,):
    return (a + b + c)
    return (a * b * c)

a = int(input('Enter 1st number: '))
b = int(input('Enter 2nd number: '))
c = int(input('Enter 3rd number: '))

print(f' sum of the numbers is equal to: {sum(a, b, c)}')
print(f' multiplication of numbers is equal to: {int(a * b * c)}')

原则上,它工作正常,但它不采用我在开始时设置的那条线:

return (a + b + c)
return (a * b * c)

我什至尝试过这样

def sum(a, b, c,):
    return (a + b + c), (a * b * c)
a = int(input('Enter 1st number: '))
b = int(input('Enter 2nd number: '))
c = int(input('Enter 3rd number: '))

print(f' sum of the numbers is equal to: {sum(a, b, c)}')
print(f' multiplication of numbers is equal to: {int(a * b * c)}')

问题: 如何在每一行上分别显示我应该调用哪个命令? 它应该是一个整数,而不是一个字符串。 也许需要编写一个程序以不同的方式输入数字?

这就是结果

  1. 输入第一个数字:5
  2. 输入第二个数字:5
  3. 输入第三个数字:5

我的第二个选项是这样的

数字之和等于:15,125

数字相乘等于:125

但我期待这个

数字之和等于:15,

数字相乘等于:125

python task
1个回答
0
投票

return
命令的含义是给出结果并结束程序。含义:当有返回命令时,执行返回命令,函数完成。 (即,
return
命令之后的下一行将不会被执行。因此程序将在第一个
return
命令处停止,第二个
return
命令将不会被执行。所以,你可以这样做:

def calc(a, b, c):
    add = a + b + c
    product = a*b*c
    result = f'''
sum of the numbers is equal to:{add},
multiplication of numbers is equal to:{product}
'''
    return result

r = calc(3,4,5)
print(r)

'''
output:
sum of the numbers is equal to:12,
multiplication of numbers is equal to:60
'''
© www.soinside.com 2019 - 2024. All rights reserved.