【问题标题】:User input and retrieving it in Python用户输入并在 Python 中检索它
【发布时间】:2016-08-30 20:34:59
【问题描述】:

我目前正在尝试在 python 中使用两个模块,我正在尝试让一个模块获取用户输入并存储它。但我不知道如何从输入模块中检索数据。我已经导入了所有内容,但这只会从输入模块中的 def 再次运行我的输入。下面是我的输入模块的一个例子。如何从用户那里返回这个值并在我的其他模块中使用它来进行计算?

def test():
   test = int(input("numbers:"))
   return test

【问题讨论】:

  • 需要更具体。其他模块是什么样的,什么不工作?
  • 不太清楚你的意思。什么是:“这只会从输入模块中的 def 再次运行我的输入”?代码说明了它应该做什么(python3),但我建议你应该决定如何处理不正确的输入。

标签: python python-2.7 python-3.x


【解决方案1】:

我假设您的意思是您使用了两个文件,并且您需要在另一个文件上使用来自测试功能的信息。然后,假设你的测试文件名是testfile.py,我会选择:

import testfile
num = testfile.test() # This will store the number recieved in the test() function in the variable num
print('Recieved number from user:', num)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多