简体   繁体   English

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

[英]How to use return value from a function and use it in the next function

I want to use the first function's value to apply on the second function that I am defining.我想使用第一个函数的值来应用我定义的第二个函数。

I defined collectUserInputTime() which takes no argument.我定义了没有参数的collectUserInputTime()

Once I called collectUserInputTime() , it does not change the value of startHour and startMinute .一旦我调用collectUserInputTime() ,它就不会更改startHourstartMinute的值。

Also, if I don't define startHour and startMinute outside of functions, I will get a startHour and startMinute undefine.另外,如果我没有在函数之外定义startHourstartMinute ,我将得到一个startHourstartMinute undefine。

I also tried to add print( startHour )inside of collectUserInputTime() , it did print out my input(), but it does not change the startHour outside of the function.我还尝试在collectUserInputTime() ) 内添加 print( startHour ) ,它确实打印了我的 input() ,但它不会更改startHour外的startHour startHour still remain as an empty string. startHour仍为空字符串。

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)

How am I able to use the return value from collectUserInputTime() which are startHour and startMinute ?我如何能够使用collectUserInputTime()的返回值,即startHourstartMinute Eventually, I can validate in my function validateUserInputTime(startHour, startMinute)最终,我可以在我的函数validateUserInputTime(startHour, startMinute)

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])

OUTPUT:输出:

13:54
13
54
Hours:  13
Minutes:  54

and so you can use it in your validateUserInputTime(startHour , startMinute) :所以你可以在你的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)
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])

First of all I would change what the function collectUserInputTime() returns.首先,我将更改函数 collectUserInputTime() 返回的内容。 I would use a list!我会使用一个列表!

Then I would use a CS concept called "Helper function": in short, you can call a function within another function.然后我会使用一个名为“Helper function”的 CS 概念:简而言之,您可以在另一个函数中调用一个函数。

The code is the following代码如下

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')

I have also made the code more "Pythonic" in few lines!我还用几行代码使代码更加“Pythonic”!

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM