在Python中,我试图在一个值回收功能中导入一个单独的模块文件,但请继续遇到错误,有什么想法吗?

发布于 2025-02-06 14:17:55 字数 710 浏览 2 评论 0原文

我正在尝试将以下模块导入到以下代码中,但是我一直在

    comput()
TypeError: comput() missing 2 required positional arguments: 'r' and 'h'

尝试使用一些不同的解决方案并观看了一些视频,但是我无法弄清楚这个想法吗?


def comput(r, h):
    if h <= 40:
        p = r * h
    else:
        p = r * 40 + (r * 1.5 * ( h - 40))
    return p


comput()

import program5_1_module


def main():
    pay()
    pay()
    pay()

def pay():

    name = input('Enter employee name: ')
    rate = float(input(f'Enter hourly rate for {name}: '))
    hours = float(input(f'Enter hours for {name} this week: '))

    pay = comput(rate, hours)
    print(f'Pay for {name} is ${pay:,.2f}')


main()

I am trying to import the following module into the below code but I keep getting

    comput()
TypeError: comput() missing 2 required positional arguments: 'r' and 'h'

I have tried some varying solutions and watched some videos but I can't figure this one out, any ideas?


def comput(r, h):
    if h <= 40:
        p = r * h
    else:
        p = r * 40 + (r * 1.5 * ( h - 40))
    return p


comput()

import program5_1_module


def main():
    pay()
    pay()
    pay()

def pay():

    name = input('Enter employee name: ')
    rate = float(input(f'Enter hourly rate for {name}: '))
    hours = float(input(f'Enter hours for {name} this week: '))

    pay = comput(rate, hours)
    print(f'Pay for {name} is ${pay:,.2f}')


main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

默嘫て 2025-02-13 14:17:56

导入另一个代码时,Python执行所有模块级代码。

在您的第一个代码中,comput()提高typeError,因为您正在使用2个参数调用函数,但您没有任何参数。

在您的第二个代码中,您导入program5_1_module(我认为这是您的第一个代码的名称?),Python在program5_1_module中执行所有代码,其中包括Comput( )

解决方案:

  • 删除comput()(第一个代码的第9行,因为它会导致错误),
  • 如果您想在第一个代码中测试调用comput()然后使用将其包装,如果__name __ ==“ __ main __”,例如,如果__name__ ==“ __ -main __”:comput(a,b)
    • 如果__name __ ==“ __ main __”告知Python解释器,导入时不应执行此操作

Python executes all module-level code when importing another code.

In your first code, comput() raises TypeError because you are calling function with 2 parameters but you gave no argument.

In your second code, you import program5_1_module(I think this is name of your first code?), and python executes all code in program5_1_module, which includes comput()

Solution:

  • Remove comput() (9th line of your first code, because it makes error)
  • If you want to test calling comput() in first code then wrap it using if __name__ == "__main__", like if __name__ == "__main__": comput(a, b).
    • if __name__ == "__main__" informs python interpreter that this should not be executed when importing
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文