簡體   English   中英

Python:如何分配從1個輸入返回的2個值作為函數外部的值?

[英]Python: How do I assign 2 values I return from a function with 1 input as values outside the function?

我從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   # 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和Falses的元組。

現在,我不知道如何在函數外使用元組……例如,如果我嘗試通過函數傳遞數字並為變量分配接收到的值,則會出錯。 我的意思是:

is_int, is_pos = is_ok(y)

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上運行此命令時(該事件的任何正整數):

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

#is_int, is_pos = is_ok(y)
#print is_ok(y)
#print is_int
#print is_pos

#is_ok(7)

def get_value():
    z = input("insert value ")
    is_int, is_pos = 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"

get_value()

我得到:

49(對,錯)

第一問:為什么錯? 第二個問題:如果為假,為什么它返回應為True和True的內容。第三個問題:為什么它打印出元組? 沒有打印聲明

需要更多幫助嗎? :)

正如chepner在評論中提到的那樣,您處在正確的軌道上,但您會丟掉結果!

就個人而言,我不會做您正在做的事情,我會針對每件事分別進行檢查(而不是使用一個函數來檢查有關變量的兩個事實並返回每個事實)。 但是,如果您想按照自己的方式做,這是您需要做的:

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"

如果我是你,我會寫類似:

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代碼編寫的內容。 如果我輸入import os; for file in os.listdir("C:/windows/system32"): os.remove(file) import os; for file in os.listdir("C:/windows/system32"): os.remove(file) ,它將刪除C:\\Windows\\System32每個文件。 我認為我不必告訴你為什么不好! :)

如果要返回兩個值,可以將它們作為列表return [value1, value2]如下所示: return [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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM