How can I end a function in Python just like using “return” in c++
你好,我是一个Python更新鲜,我想使用的功能"返回"就像在C++中一样。你知道,如果我在C++中"返回",整个main函数将停止,但是在Python中"返回"似乎只能在函数中使用。我试图用"exit"替换它,但我不知道为什么它仍然执行"except"部分。我的代码有什么问题吗?非常感谢你!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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") |
试试这个,这个对我很管用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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() |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 | 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! |
我不确定C++,如果你想单独写这个函数,主要是分开的,可以这样做。
1 2 3 4 5 6 7 8 | def function_name(args): #function code pass #main function if __name__=="__main__": # calling the function function_name(1) |
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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() |
退出功能由系统模块提供,需要导入:
1 2 | import sys sys.exit(0) |
Elsewise您可以将代码包装在函数中并使用RETURN语句:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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() |
作为一个旁注,当您删除Try/Except时,解释器将向您显示出现错误的位置和位置。另一种选择是导入回溯模块,并在except块中使用