-6

Hello I am a Python fresher and I wanna use the feature "return" just like in c++. You know that if I "return" in c++, the whole main function will stop, but it seems that in Python "return" can only be used in function. I tried to use "exit" to replace it but I don't know why it still executed the "except" part. Is there anything wrong with my code? Thank you very much!

name=input("Please input your name? ")
print("Hello,", name)
year=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:
                print("Congratulation! Fist Prize!")
                exit(0)
            elif int(luckynumber) == 88:
                print("Not bad! Second Prize! ")
                exit(0)
            else:
                print("Best luck for next turn!")
        print("Sorry, you didn't win. ")
    else:
        print("Get out!")
except:
      print("Your birth-year or luckynumber must be an integer")
6
  • 1
    "it seems that in Python "return" can only be used in function." In C++ too, so not sure what you're getting at. Commented Jul 15, 2017 at 19:41
  • Why not just put the logic inside a function (including appropriate returns) and then call the function? Commented Jul 15, 2017 at 19:46
  • 2
    Maybe you want to read a basic book about python? Or use a search engine? Commented Jul 15, 2017 at 19:46
  • I think it is a good question, 'why it still executed the "except" part.', does anyone know? Commented Jul 15, 2017 at 20:00
  • For your information: sys.exit() is ending the program by raising (an interceptable) exception. Commented Jul 15, 2017 at 20:02

2 Answers 2

0

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()
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks man! It works. And could you tell me if there is "main function" python just like in C++?
If it works can you please mark my answer as useful and right answer please ?
if name__=="__main":
@Xu Haifeng i have edited the code using the main function. please take a look and see whether that is what you are looking for. the below link has some info regaring main stackoverflow.com/questions/22492162/…
0

The exit function is provided by the sys module, which needs to be imported:

import sys
sys.exit(0)

Elsewise you can wrap your code in function and use the return statement:

def main():
    name=input("Please input your name? ")
    print("Hello,", name)
    year=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:
                    print("Congratulation! Fist Prize!")
                    return
                elif int(luckynumber) == 88:
                    print("Not bad! Second Prize! ")
                    return
                else:
                    print("Best luck for next turn!")
            print("Sorry, you didn't win. ")
        else:
            print("Get out!")
    except:
          print("Your birth-year or luckynumber must be an integer")

if __name__ == '__main__':
    main()

As a sidenote when you remove try/except, the interpreter is going to show you which and where an error appeared.
Another option would be to import the traceback module and use traceback.print_exec() in the except block.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.