-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")
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
Xu Haifeng
  • 31
  • 3
  • 1
    "it seems that in Python "return" can only be used in function." In C++ too, so not sure what you're getting at. – Rakete1111 Jul 15 '17 at 19:41
  • Why not just put the logic inside a function (including appropriate `return`s) and then call the function? – MSeifert Jul 15 '17 at 19:46
  • 2
    Maybe you want to read a basic book about python? Or use a search engine? – GhostCat Jul 15 '17 at 19:46
  • I think it is a good question, '_why it still executed the "except" part._', does anyone know? – jas Jul 15 '17 at 20:00
  • For your information: `sys.exit()` is ending the program by raising (an interceptable) exception. – Klaus D. Jul 15 '17 at 20:02
  • `return` can only be used inside a function and, just like in C++, it will cause control to return to the function's caller. In your case, the code executing isn't inside a function, so there's no caller to return to—all that can be done is to stop running by calling `exit()` to go back to the Operating System (or whatever invoked Python and your script). – martineau Jul 15 '17 at 22:40

2 Answers2

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.

Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
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()