gpt4 book ai didi

python - 如何检查是否缺少参数

转载 作者:太空宇宙 更新时间:2023-11-04 03:36:32 25 4
gpt4 key购买 nike

我写了这段代码:

def suma(num1,num2):

if(isinstance(num1,int)) or (isinstance(num2,int)) :
if num1<0 or num2<0:
print("iavo")
elif num1!=int(num1)or num2!=int(num2):
print("iao")
else:
suma_a = num1+num2
return suma_a
else:
print("ia")

我需要检查是否有 num1num2。如果没有,程序应该打印出缺少参数。

最佳答案

如果您没有分配默认值并且用户没有提供两个参数,您将在进入函数之前得到一个TypeError: suma() missing 1 required positional argument:

def suma(num1=None,num2=None):
# make sure user has entered two args and both are numbers
if num1 is None or num2 is None:
return "You Must choose two numbers"
# rest of code

关于python - 如何检查是否缺少参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28801699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com