【问题标题】:Using Return From One Function To Another Python使用从一个函数返回到另一个 Python
【发布时间】:2017-06-30 20:39:55
【问题描述】:

这是我的代码:

def options():
    wanted_float = float(input("Wanted Float: "))
    specificity = float(input("How close to float (ex: .001): "))
    low_output = float(input("Min Float of Output Skin: "))
    high_output = float(input("Max Float of Output Skin: "))
    needed_average = ((wanted_float-low_output)/(high_output-low_output))
    print("Needed average: ", needed_average)
    only_lower = input("Only show floats lower than previous? yes/no: ")
    which = input("Would you like to load floats manually or automatically? (manual/automatic): ")
    return which

def mode(which):
    if (mode == 'manual'):
        print("Manual")

    if (mode == 'automatic'):
        print("automatic")

def start():
    options()
    mode(which)

start()

但是,我不断收到错误消息。我查看了其他一些处理此问题的回复,但它们似乎不适用于此。

Wanted Float: .5
How close to float (ex: .001): .001
Min Float of Output Skin: 0
Max Float of Output Skin: 1
Needed average:  0.5
Only show floats lower than previous? yes/no: yes
Would you like to load floats manually or automatically? (manual/automatic): manual
Traceback (most recent call last):
  File "C:\Users\.Anderson\Documents\Python\floats\organized.py", line 172, in <module>
    start()
  File "C:\Users\.Anderson\Documents\Python\floats\organized.py", line 161, in start
    mode(which)
NameError: name 'which' is not defined

它说第 172 行和第 161 行的原因是因为我之间还有很多其他代码,但我只是在开始时调用选项和模式,这是当前所有代码所做的

【问题讨论】:

  • 只需执行mode(options())。您可能想了解变量和范围
  • 什么错误?请发布堆栈跟踪。
  • which = options()
  • @NoticeMeSenpai 我添加了错误

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


【解决方案1】:

问题是你没有保存options函数的返回值。

你可以这样做:

mode(options()) 

which = options()
mode(which)

【讨论】:

    猜你喜欢
    • 2021-12-16
    • 2015-07-16
    • 2021-11-02
    • 2020-03-27
    • 1970-01-01
    • 2019-03-29
    • 2021-08-14
    • 2023-01-28
    • 1970-01-01
    相关资源
    最近更新 更多