在Python中,我试图在一个值回收功能中导入一个单独的模块文件,但请继续遇到错误,有什么想法吗?
我正在尝试将以下模块导入到以下代码中,但是我一直在
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
导入另一个代码时,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()
raisesTypeError
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 inprogram5_1_module
, which includescomput()
Solution:
comput()
(9th line of your first code, because it makes error)comput()
in first code then wrap it usingif __name__ == "__main__"
, likeif __name__ == "__main__": comput(a, b)
.if __name__ == "__main__"
informs python interpreter that this should not be executed when importing