Python:如何分配从函数返回的2个值,其中1个输入作为函数外部的值?
我从python开始,认为尝试构建一个函数来确定传递给它的输入是否为整数,如果是,则确定它是否为正,这将是一个很好的练习。在稍后的阶段,我计划从只接受正整数的数学函数中引用该函数 无论如何,这是我构建的函数:Python:如何分配从函数返回的2个值,其中1个输入作为函数外部的值?,python,function,scope,return,tuples,Python,Function,Scope,Return,Tuples,我从python开始,认为尝试构建一个函数来确定传递给它的输入是否为整数,如果是,则确定它是否为正,这将是一个很好的练习。在稍后的阶段,我计划从只接受正整数的数学函数中引用该函数 无论如何,这是我构建的函数: def is_ok(x): #checks if x is a positive integer is_int = False is_pos = False if type(x) == int: is_int = True #
def is_ok(x): #checks if x is a positive integer
is_int = False
is_pos = False
if type(x) == int:
is_int = True # if it's an integer, continue to test if >0
if x > 0:
is_pos = True
else:
pass
else:
pass
return is_int, is_pos
如您所见,它获取一个参数(要测试的值),并返回一个true和false的元组
现在,我不知道如何在函数外使用元组。。。例如,如果我试图通过函数传递一个数字,并将收到的值赋给变量,就会得到一个错误。我的意思是:
is_int, is_pos = is_ok(y)
def get_value():
while True:
z = input("Insert value: ") # DON'T DO THIS, INPUT() IS BADDDDD IN PYTHON2
# instead do:
## try: z = int(raw_input("Insert value: "))
## except ValueError:
## print("Please provide an integer")
## continue
# this also FORCES z to be an integer, so isinstance(z,int) is ALWAYS TRUE
is_int = isinstance(z,int)
is_pos = is_int and z>0 # this will keep you from throwing an exception if
# you can't compare z>0 because z isn't int
if is_int and is_pos:
print z**2
elif is_int:
print "Please provide an integer"
continue
else:
print "Integer must be positive"
continue
# nothing can ever get to that last else block you had.
例如,y是某个数字。这会在运行时产生错误
因此,基本上我想了解的是如何做到以下几点:
函数A接受一个值作为其唯一输入==>通过is_ok(x)函数运行它==>函数A获取由is_ok()函数生成的元组,并使用if/elifs根据元组的值生成不同的结果
函数A的示例:
def get_value():
z = input("insert value" ")
is_ok(z)
if (is_int,is_pos) == (True, True):
print z**2
elif (is_int,is_pos) == (False, False):
print "Please enter an integer"
get_value()
elif (is_int, is_pos) == (True, False):
print "Please enter an positive integer"
get_value()
else:
print "something is wrong"
谢谢你的帮助!
谢谢
======================
编辑:延迟添加
例如,当我在输入7上运行时(任何正整数):
我得到:
49
(对,错)
第一个问题:为什么是假的?
第二个问题:如果为假,为什么它会返回它应该为真的东西
第三个问题:为什么要打印元组?无打印语句
更多帮助?:) 正如切普纳在评论中提到的那样,你走在正确的道路上,但你正在放弃你的结果 就个人而言,我不会做你正在做的事情,我会分别检查每件事(而不是使用一个函数检查变量的两个事实并返回每个事实)。但如果你想按自己的方式做,以下是你需要做的:
def is_ok(x): #checks if x is a positive integer
is_int = False
is_pos = False
if type(x) == int:
is_int = True # if it's an integer, continue to test if >0
if x > 0:
is_pos = True
else:
pass
else:
pass
return is_int, is_pos
def get_value():
z = input("insert value: ")
is_int, is_pos = is_ok(z)
if (is_int,is_pos) == (True, True): # could also do `if all(is_int,is_pos):`
print z**2
elif (is_int,is_pos) == (False, False):
print "Please enter an integer"
get_value()
elif (is_int, is_pos) == (True, False):
print "Please enter an positive integer"
get_value()
else:
print "something is wrong"
如果我是你,我会写下这样的话:
is_int, is_pos = is_ok(y)
def get_value():
while True:
z = input("Insert value: ") # DON'T DO THIS, INPUT() IS BADDDDD IN PYTHON2
# instead do:
## try: z = int(raw_input("Insert value: "))
## except ValueError:
## print("Please provide an integer")
## continue
# this also FORCES z to be an integer, so isinstance(z,int) is ALWAYS TRUE
is_int = isinstance(z,int)
is_pos = is_int and z>0 # this will keep you from throwing an exception if
# you can't compare z>0 because z isn't int
if is_int and is_pos:
print z**2
elif is_int:
print "Please provide an integer"
continue
else:
print "Integer must be positive"
continue
# nothing can ever get to that last else block you had.
我们在Python2中不使用
input
的原因是它执行您作为Python代码编写的任何内容。如果我输入了导入操作系统;对于os.listdir(“C:/windows/system32”):os.remove(file)
中的文件,它将删除C:\windows\system32
中的每个文件。我想我不必告诉你为什么那样不好!:) 如果要返回两个值,可以将它们作为如下列表返回:
返回[value1,value2]
如果您的目标是验证输入以确保其为正整数,我将这样做:
def get_value():
while True:
try:
z = int(raw_input("insert value: "))
if z > 0:
return z
else:
print "Please enter a positive integer"
except ValueError:
print "Please enter an integer"
你有什么错误?您显示的示例调用是正确的。但是,在
get_value()
中,您无法将is_ok()
的返回值分配给is_int
和is_pos
。感谢您的发现。首先,基本上当我传递一个正整数时,我得到的元组是(True,False)。顺便说一句,现在我还发现了一个问题,当用户试图传递字符串时,我会遇到这个问题。请注意我的编辑。谢谢好东西@adsmith,好东西。另外,我认为了解输入的陷阱很重要:D非常感谢!你介意看看我对原始帖子的编辑(在底部),看看你是否能理解我为什么得到这些输出?再次感谢:)@Optimesh我一行一行地复制了您的代码,但我没有得到与您相同的输出。我想你需要再检查一下你的本地代码:)奇怪!无论如何,谢谢你:)(是的,现在看起来好多了。难以置信!!)