【问题标题】:Python -- how to add an input function under a differently named functionPython——如何在不同命名的函数下添加输入函数
【发布时间】:2020-10-07 03:25:56
【问题描述】:

所以,我只是想写一个简单的代码来根据温度高于或低于 70 度来确定“是否需要夹克”。

我可以简单地使用我找到的一个示例,它工作得很好(下)但是,我需要在一个名为 Is_a_Jacket_needed() 的函数下使用它,并且每次我尝试将输入放入正文中或只是尝试添加输入命令以在所需函数标题的括号中请求临时,我无法让它工作。 (语法错误、需要缩进块等)但即使我尝试更改这些内容,我也没有收到问题,因此对我没有帮助。

(我用来获取答案的代码在这里 --- 再次,我只需要在 def Is_Jacket_Needed(): 中使用它)

temperature = int(input('What is the temperature? '))
if temperature > 70:
    print('No need for a Jacket!')
else:
    print('A Jacket is Needed')

提前非常感谢!

【问题讨论】:

    标签: python function input


    【解决方案1】:

    我不知道你遇到了什么错误:

    test.py

    def is_jacket_needed():
        temperature = int(input('What is the temperature? '))
        if temperature > 70:
            print('No need for a Jacket!')
        else:
            print('A Jacket is Needed')   
    
    >>> from test import is_jacket_needed
    >>> is_jacket_needed()
    What is the temperature? 50
    A Jacket is Needed
    >>> is_jacket_needed()
    What is the temperature? 80
    No need for a Jacket!
    

    【讨论】:

    • 谢谢你们!!很有帮助。我只需要添加 (is_jacket_needed() ) 来调用该函数。非常感谢!
    【解决方案2】:

    我相信您忘记将参数温度添加到您的函数中。

    def is_a_Jacket_needed():
        temperature = int(input("What is the temperature?"))
        if temperature > 70:
            print("No need for a Jacket!")
        else:
            print("A Jacket is Needed")
    
    #Then call your function
    
    is_a_Jacket_needed(70)
    is_a_Jacket_needed(75)
    

    【讨论】:

    • 您的 temperature 参数正在被函数体内的 temperature 覆盖。
    • 你是对的,我现在在 python 中运行一些代码,我无法测试它,我忘记了我调用了输入函数。
    猜你喜欢
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    相关资源
    最近更新 更多