如何使用函数的返回值并在下一个函数中使用它

时间:2019-02-04 08:54:28

标签: python python-3.x

我想使用第一个函数的值应用于我定义的第二个函数。

我定义了collectUserInputTime(),它不带参数。

一旦我致电collectUserInputTime(),它就不会更改startHourstartMinute的值。

此外,如果我没有在函数之外定义startHourstartMinute,则会得到startHourstartMinute的未定义。

我还尝试在startHour内添加print(collectUserInputTime()),它确实打印了我的input(),但并未在函数外部更改startHourstartHour仍然是一个空字符串。

startHour = ''
startMinute = ''
def collectUserInputTime():
     print('Enter the time the call starts in 24-hour rotation:')
     data = input()
     astop = data.find(':')
     startHour = data[0:astop]
     startMinute = data[astop+1:]
     return startHour and startMinute



def validateUserInputTime(startHour , startMinute):
    if (startHour in hoursList and startMinute in minutesList):
        print('it is valid')
    else:
        print('input invalid')

collectUserInputTime()
validateUserInputTime(startHour, startMinute)

如何使用collectUserInputTime()startHour的{​​{1}}返回值?最终,我可以在函数startMinute

中进行验证

3 个答案:

答案 0 :(得分:0)

startHour = ''
startMinute = ''
def collectUserInputTime():
     print('Enter the time the call starts in 24-hour rotation:')
     data = input()
     astop = data.find(':')
     startHour = data[0:astop]
     startMinute = data[astop+1:]
     print(startHour)
     print(startMinute)
     return startHour, startMinute

ret_val = collectUserInputTime()
print("Hours: ", ret_val[0])
print("Minutes: ", ret_val[1])

输出:

13:54
13
54
Hours:  13
Minutes:  54

,因此您可以在validateUserInputTime(startHour , startMinute)中使用它:

def validateUserInputTime(startHour , startMinute):
    if (startHour in hoursList and startMinute in minutesList):
        print('it is valid')
    else:
        print('input invalid')

ret_val = collectUserInputTime()
startHour = ret_val[0]
startMinute = ret_val[1]
validateUserInputTime(startHour, startMinute)

答案 1 :(得分:0)

startHour = ''
startMinute = ''
def collectUserInputTime():
     print('Enter the time the call starts in 24-hour rotation:')
     data = input()
     astop = data.find(':')
     startHour = data[0:astop]
     startMinute = data[astop+1:]
     return (startHour, startMinute)



def validateUserInputTime(startHour , startMinute):
    if (startHour in hoursList and startMinute in minutesList):
        print('it is valid')
    else:
        print('input invalid')

rtn_data = collectUserInputTime()
validateUserInputTime(rtn_data[0], rtn_data[1])

答案 2 :(得分:0)

首先,我将更改函数collectUserInputTime()返回的内容。我会用一个清单!

然后,我将使用一个称为“ Helper function”的CS概念:简而言之,您可以在另一个函数中调用一个函数。

代码如下

def collectUserInputTime():
 data = input('Enter the time the call starts in 24-hour rotation: ')
 astop = data.find(':')
 startHour = data[0:astop]
 startMinute = data[astop+1:]
 return [startHour, startMinute]

def validateUserInputTime():
 hour = collectUserInputTime()
 startHour, startMinute = int(hour[0]), int(hour[1]) # if lists are int_lists
 if (startHour in hoursList) and (startMinute in minutesList):
     print('It is valid')
 else:
     print('Input invalid')

我还在几行代码中使代码更加“ Pythonic”!

希望这会有所帮助