【问题标题】:python 3.5.1 won't return strings from functionspython 3.5.1 不会从函数返回字符串
【发布时间】:2016-03-23 14:02:23
【问题描述】:

我对 python 还是很陌生,而且我主要是在玩游戏来尝试自学。

我有一个或多或少像这样的函数:

str1 = "a"
str2 = "b"
str3 = "c"
thing = random.randint(1,2)
def fun():
  if(thing == 1):
     strVal = str1 + str2
  elif(thing == 2):
     strVal = str2 + str3
  return strVal
outp = fun

但是当我尝试将print(outp) 发送到控制台时,我得到的输出类似于<function fun at 0x000001F29138AAE8> 而不是"ab""bc" 我如何获得 fun 的字符串结果打印而不是打印什么?我搜索如何解决这个问题只购买了 print() 函数的文档和示例,这并不是我真正想要的。

【问题讨论】:

  • 你必须调用函数,就像你使用randint...

标签: function python-3.x printing


【解决方案1】:

在您的代码中,您不会调用您的函数。如下调用一个函数

outp = fun()

这就是为什么您会在 0x000001F29138AAE8 处看到 函数乐趣。基本上,Python 会告诉您变量 outp 是指向地址 0x000001F29138AAE8 处的函数的指针。

【讨论】:

  • 感谢您指出这一点。在你和@jonrsharpe 之间,我现在知道我在做什么
猜你喜欢
  • 1970-01-01
  • 2014-12-07
  • 2017-05-05
  • 1970-01-01
  • 1970-01-01
  • 2010-12-14
  • 2014-11-06
  • 1970-01-01
相关资源
最近更新 更多