简体   繁体   中英

Defining functions

I have this code which is meant to return true or false if the parrot is talking and at what time. This bit works perfectly and it returns the value correctly but what i want to do is instead of it returning a true or false value i want it to return a print statement.

def parrot_talking(talking, hour):

    if talking and (hour < 7 or hour >20):
        return True
    else:
        return False
parrot1 = parrot_talking(True, 8)

print(parrot1)

This is the bit that works fine but the next bit of the code keeps giving me "NONE" when i run it.

def parrot_talking(talking, hour):

    if talking and (hour < 7 or hour >20):
        print("Trouble")
    else:
        print("Its fine")
parrot1 = parrot_talking(True, 5)

print(parrot1)

and this is what it should return

#parrot_trouble(True, 6) → True
#parrot_trouble(True, 7) → False
#parrot_trouble(False, 6) → False

any help will be much appreciated

I'm not sure this is what you need, but you can modify your function to return boolean value while keeping print statement:

def parrot_talking(talking, hour):    
    if talking and (hour < 7 or hour >20):
        print("Trouble")
        return True;
    else:
        print("Its fine")
        return False    
parrot1 = parrot_talking(True, 5)    
print(parrot1)

or you can return string value instead:

def parrot_talking(talking, hour):    
    if talking and (hour < 7 or hour >20):
        return "Trouble"
    else:
        return "Its fine"    
parrot1 = parrot_talking(True, 5)    
print(parrot1)

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