[Solved] How can I end a function in Python just like using “return” in c++ [closed]


Try this, this worked fine for me

def my_func():
    name=raw_input("Please input your name? ")
    print("Hello,", name)
    year=raw_input("Please input your birth year? ")
    try:
        age=2007-int(year)
        if age <= 25:
            print("Welcome home! You have 5 chances to win the prize. ")
            for i in range (1, 5):
                luckynumber=input("Please input an random number? ")
                if int(luckynumber) == 66:
                    return("Congratulation! Fist Prize!")
                elif int(luckynumber) == 88:
                    return("Not bad! Second Prize! ")
                else:
                    print("Best luck for next turn!")
            return("Sorry, you didn't win. ")
        else:
            return("Get out!")
    except:
        return("Your birth-year or luckynumber must be an integer")

print my_func()

Output:

Please input your name? Stackoverflow
('Hello,', 'Stackoverflow')
Please input your birth year? 1985
Welcome home! You have 5 chances to win the prize. 
Please input an random number? 25
Best luck for next turn!
Please input an random number? 35
Best luck for next turn!
Please input an random number? 45
Best luck for next turn!
Please input an random number? 66
Congratulation! Fist Prize!

I am not sure of C++ if you want to write the function separately and main separately it can be done like this

def function_name(args):
    #function code
    pass

#main function
if __name__=="__main__":
    # calling the function
    function_name(1)

Example:

def my_func():
    name=raw_input("Please input your name? ")
    print("Hello,", name)
    year=raw_input("Please input your birth year? ")
    try:
        age=2007-int(year)
        if age <= 25:
            print("Welcome home! You have 5 chances to win the prize. ")
            for i in range (1, 5):
                luckynumber=input("Please input an random number? ")
                if int(luckynumber) == 66:
                    return("Congratulation! Fist Prize!")
                elif int(luckynumber) == 88:
                    return("Not bad! Second Prize! ")
                else:
                    print("Best luck for next turn!")
            return("Sorry, you didn't win. ")
        else:
            return("Get out!")
    except:
        return("Your birth-year or luckynumber must be an integer")

if __name__=="__main__":
    print my_func()

6

solved How can I end a function in Python just like using “return” in c++ [closed]