简体   繁体   中英

Python3 Using functions inside other functions?

I'm a bit confused as to how this is supposed to work. For example,

I have these two functions that I've written:

def in_range(par):
    if (par >= 50) and (par <= 100):
        print(True)
    else:
        print(False)



def squares_in_range(twoargument):
    for a in range(3, 20):
        b = (a*a)
        print(b, end="")
        if a<19:
          print(end=",")

Now, I would like to use the first function in the second so to say. It should check if the numbers in the second function are within the 50-100 range and then print out "True" if its within and "False" if out of range for each number so that it becomes a list that might look like this: True, False, False, True... and so on.

How do I go about this?

edit: I am referring to "b" in the second function, not "a". I've tried calling the function but nothing happens. I guess that's because the first functions does not use "return"?

只需在第二个函数中的某个地方编写in_range(b)

It's not clear to me which numbers, a or b, in the second function you were referring to.

This gets you a list of booleans for checking whether the numbers from a is within the range. You can modify it to check for b.

def in_range(par,alist):
  if (par >= 50) and (par <= 100):
      print(True)
      alist.append(True)
  else:
      print(False)
      alist.append(False)



def squares_in_range(twoargument):
    within_range=[]
    for a in range(3, 20):
        in_range(a,within_range)
        b = (a*a)
        print(b)
        if a<19:
          print(",")
    print(within_range) # print the final list of booleans

squares_in_range(1) #testing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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