Python 2.7 使用函数时,请尝试不返回我认为应该返回的值

Python 2.7 使用函数时,请尝试不返回我认为应该返回的值,python-2.7,Python 2.7,我正在做一些家庭作业练习,试图用Try检查一个数字是否为正整数。在这个过程中的某个地方,它返回我输入的值,但它返回负值,即使我最终输入了一个有效值。 比方说-首先我输入了-10,它告诉我再输入一次,然后我输入2,但返回值仍然是-10 def takes_desicion(name): print "Ok %s, now lets make a desicion what to do with your text.\n" %name print "you can either- 1

我正在做一些家庭作业练习,试图用Try检查一个数字是否为正整数。在这个过程中的某个地方,它返回我输入的值,但它返回负值,即使我最终输入了一个有效值。 比方说-首先我输入了-10,它告诉我再输入一次,然后我输入2,但返回值仍然是-10

def takes_desicion(name):
    print "Ok %s, now lets make a desicion what to do with your text.\n" %name
    print "you can either- 1.leave it, 2.reverse it, or 3.make a new input."
    while True:
        try:    #takes an input with exception- in this case, only integer.
            desicion = int(raw_input("Hit your number to decide."))
            print desicion
        except ValueError:
            print "Sorry, thats not a number."
            continue
        else:   #if int is negetive, goes back to the function again, recursively
            if desicion < 1:
                print "That number is illegal."
                takes_desicion(name)
            break
    return desicion
def接受决策(名称):
打印“确定%s,现在让我们决定如何处理文本。\n”%name
打印“您可以-1.离开它,2.反转它,或3.进行新的输入。”
尽管如此:
try:#接受带异常的输入-在本例中,仅接受整数。
desicion=int(原始输入(“点击您的号码决定”)
印刷决策
除值错误外:
打印“对不起,那不是一个数字。”
持续
else:#如果int是负的,则以递归方式再次返回该函数
如果决策<1:
打印“该号码是非法的。”
采取行动(姓名)
打破
返回决策

如果我的第一个输入是-10,第二个输入是2,我希望输出(返回)是2。但是它是-10。这是从函数返回的值。

您在最后第三行再次调用
takes\u descion
函数,但不使用该函数的值执行任何操作。您需要存储新返回的值:
descion=takes\u descion(name)

虽然您应该只使用
continue
而不是在
循环时跳转到
的第一个类似项,但无需再次调用该函数:

def takes_desicion(name):
    print "Ok %s, now lets make a desicion what to do with your text.\n" %name
    print "you can either- 1.leave it, 2.reverse it, or 3.make a new input."
    while True:
        try:    #takes an input with exception- in this case, only integer.
            desicion = int(raw_input("Hit your number to decide."))
            print desicion
        except ValueError:
            print "Sorry, thats not a number."
            continue
        else:   #if int is negetive, goes back to the function again, recursively
            if desicion < 1:
                print "That number is illegal."
                continue
            break
    return desicion
def接受决策(名称):
打印“确定%s,现在让我们决定如何处理文本。\n”%name
打印“您可以-1.离开它,2.反转它,或3.进行新的输入。”
尽管如此:
try:#接受带异常的输入-在本例中,仅接受整数。
desicion=int(原始输入(“点击您的号码决定”)
印刷决策
除值错误外:
打印“对不起,那不是一个数字。”
持续
else:#如果int是负的,则以递归方式再次返回该函数
如果决策<1:
打印“该号码是非法的。”
持续
打破
返回决策

在最后第三行再次调用
takes\u descion
函数,但不使用该函数的值执行任何操作。您需要存储新返回的值:
descion=takes\u descion(name)

虽然您应该只使用
continue
而不是在
循环时跳转到
的第一个类似项,但无需再次调用该函数:

def takes_desicion(name):
    print "Ok %s, now lets make a desicion what to do with your text.\n" %name
    print "you can either- 1.leave it, 2.reverse it, or 3.make a new input."
    while True:
        try:    #takes an input with exception- in this case, only integer.
            desicion = int(raw_input("Hit your number to decide."))
            print desicion
        except ValueError:
            print "Sorry, thats not a number."
            continue
        else:   #if int is negetive, goes back to the function again, recursively
            if desicion < 1:
                print "That number is illegal."
                continue
            break
    return desicion
def接受决策(名称):
打印“确定%s,现在让我们决定如何处理文本。\n”%name
打印“您可以-1.离开它,2.反转它,或3.进行新的输入。”
尽管如此:
try:#接受带异常的输入-在本例中,仅接受整数。
desicion=int(原始输入(“点击您的号码决定”)
印刷决策
除值错误外:
打印“对不起,那不是一个数字。”
持续
else:#如果int是负的,则以递归方式再次返回该函数
如果决策<1:
打印“该号码是非法的。”
持续
打破
返回决策
另请参见。另请参见。