当我运行我的代码时,没有任何返回值,但没有错误,并且我调用了方法。

huangapple go评论76阅读模式
英文:

When I run my code, nothing is returned, yet there are no errors and I called to the methods

问题

我调用了所有的方法,所以我不确定为什么它们不会返回任何值?

这是我的尝试:

train_mass = 22680
train_acceleration = 10
bomb_mass = 1

def f_to_c(f_temp):
  c_temp = (f_temp - 32) * 5/9
  return c_temp
f100_in_celsius = f_to_c(100)
def c_to_f(c_temp):
  f_temp = c_temp * (9/5) +32
  return f_temp
c0_in_fahrenheit = c_to_f(0)

def get_force(mass, acceleration):
  return mass*acceleration
train_force = get_force(train_mass, train_acceleration)

def get_energy(mass, c=3*10**8):
  return mass * c**2
bomb_energy = get_energy(bomb_mass)
英文:

I called all the methods so I'm not sure why they won't return anything?

this is my attempt:

train_mass = 22680
train_acceleration = 10
bomb_mass = 1

def f_to_c(f_temp):
  c_temp = (f_temp - 32) * 5/9
  return c_temp
f100_in_celsius = f_to_c(100)
def c_to_f(c_temp):
  f_temp = c_temp * (9/5) +32
  return f_temp
c0_in_fahrenheight = c_to_f(0)

def get_force(mass, acceleration):
  return mass*acceleration
train_force = get_force(train_mass, train_acceleration)

def get_energy(mass, c=3*10**8):
  return mass * c**2
bomb_energy = get_energy(bomb_mass)

I was expecting the values in the method to be returned but nothing gets returned. Also, there were no errors.

答案1

得分: 1

来自Python Principles

> ## 在Python中的打印与返回
>
> 打印(print)和返回(return)是完全不同的概念。
>
> print 是一个你调用的函数。调用 print 会立即让你的程序输出文本,供人类查看。当你想向人显示一个值时,请使用 print
>
> return 是一个关键字。当到达 return 语句时,Python 将停止当前函数的执行,并将一个值发送到调用函数的地方。当你想要从代码的一个地方发送一个值到另一个地方时,请使用 return
>
> 使用 return 会改变程序的流程。使用 print 则不会。

英文:

From Python Principles:

> ## Print vs. return in Python
>
> Printing and returning are completely different concepts.
>
> print is a function you call. Calling print will immediately make your
> program write out text for you to see. Use print when you want to show
> a value to a human.
>
> return is a keyword. When a return statement is reached, Python will
> stop the execution of the current function, sending a value out to
> where the function was called. Use return when you want to send a
> value from one point in your code to another.
>
> Using return changes the flow of the program. Using print does not.

答案2

得分: 0

如上所述,请使用打印语句:

train_mass = 22680
train_acceleration = 10
bomb_mass = 1

def f_to_c(f_temp):
  c_temp = (f_temp - 32) * 5/9
  return c_temp

f100_in_celsius = f_to_c(100)
print(f100_in_celsius)

def c_to_f(c_temp):
  f_temp = c_temp * (9/5) +32
  return f_temp

c0_in_fahrenheit = c_to_f(0)
print(c0_in_fahrenheit)

def get_force(mass, acceleration):
  return mass * acceleration

train_force = get_force(train_mass, train_acceleration)
print(train_force)

def get_energy(mass, c=3*10**8):
  return mass * c**2

bomb_energy = get_energy(bomb_mass)
print(bomb_energy)
英文:

As mentioned above, use print statements:

train_mass = 22680
train_acceleration = 10
bomb_mass = 1

def f_to_c(f_temp):
  c_temp = (f_temp - 32) * 5/9
  return c_temp

f100_in_celsius = f_to_c(100)
print(f100_in_celsius)

def c_to_f(c_temp):
  f_temp = c_temp * (9/5) +32
  return f_temp

c0_in_fahrenheit = c_to_f(0)
print(c0_in_fahrenheit)

def get_force(mass, acceleration):
  return mass * acceleration

train_force = get_force(train_mass, train_acceleration)
print(train_force)

def get_energy(mass, c=3*10**8):
  return mass * c**2

bomb_energy = get_energy(bomb_mass)
print(bomb_energy)

huangapple
  • 本文由 发表于 2023年7月10日 10:55:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定