如何从 if 语句内部 return 到程序开头?

How to return to beginning of program from inside of if statement?

我正在练习一些基本的编码,我是 运行 一个简单的数学程序 运行 在终端上的 Visual Studio 代码。

如何在程序开头创建 return 选项,或者在陷入 if 语句后退出程序?

示例:

#beginning of program

user_input=input('Please select "this" or "that": ')
findings=user_input

If findings == this:
        print(this)
        # How can I redirect back to first user input question, instead 
        # of just ending here? 
if findings == that:
        print (that)
        # Again, How do I redirect back to first user input, instead of 
        # the program ending here?  
# Can I setup a Play_again here with options to return to user_input, 
        # or exit program? And then have all other If statements 
        # redirect here after completion? How would I do that? with 
        # another If? or with a for loop? 
#end program

您可以尝试将整个程序包装在一个 while 循环中,如下所示:

while(True):
    user_input=input('Please select "this" or "that": ')
    this = 'foo'
    if user_input == this:
        print(this)
        continue 

    if user_input == this:
        print(this)
        continue    

不幸的是,'another technique' 我想使用它并没有奏效。

代码如下(修改第一个例子):

import sys

def main():
    # Lots of setup code here.
    def start_over():
        return #Do nothing and continue from the next line
    condition = 1==1 #Just a sample condition, replace it with "check(condition)".
    float_condition = condition

    def play(*just_define:bool):
        if not just_define:
            play_again = input('play again? "y" or "n"')
    
            if play_again == 'y':
                start_over() #Jump to the beginning of the prohram.
            if play_again == 'n':
                sys.exit() #Exit the program
        
    while True:
        if float_condition == True:
            # print(float_condition)
            play() #skip to play_again from here?
    
        if float_condition == False:
            #print(float_condition)
            play() #skip to play_again from here?

    #I removed the extra "main()" which was here because it'd cause an infinite loop-like error.

main()

输出:

play again? "y" or "n"y
play again? "y" or "n"n

Process finished with exit code 0

play(*just_define:bool)函数中的*使just_define参数可选。如果您想 only 告诉 Python 搜索此函数,请使用该参数,这样它就不会抛出 ReferenceError 并且不会执行该行之后的任何内容if not just_define:。如何调用:play(just_define=True).

我用过nested functions。我已经 defined play 以便您可以从代码中的其他地方调用它。

感谢@Hack3r - 我终于能够选择 return 回到程序的开头或退出。但这导致了一个新问题。现在我的 print(results) 打印了 4 或 5 次...

这是我构建并正在使用的实际代码:

def main():

    math_Options=['Addition +','Subtraction -','Multiplication *','Division /']
    math_func=['+','-','*','/']
    for options in math_Options:
        print(options)
    print('Lets do some Math! What math function would you like to use? ')
   
    while True:
        my_Math_Function = input('Please make your choice from list above using the function symbol: ') 
        my_Number1=input('Please select your first number: ')
        x=float(my_Number1)
        print('Your 1st # is: ', x)
        my_Number2=input('Please select your Second Number: ')
        y=float(my_Number2)
        print('Your 2nd # is: ', y)
        z=float()
        print('')
        for Math_function in math_func:
            if my_Math_Function == math_func[0]:
                z=x+y
            if my_Math_Function == math_func[1]:
                z=x-y
            if my_Math_Function == math_func[2]:
                z=x*y
            if my_Math_Function == math_func[3]:
                z=x/y

            if (z % 2) == 0 and z>0:    
                print(z, ' Is an EVEN POSITIVE Number')
            if (z % 2) == 1 and z>0:    
                print(z, ' IS a ODD POSTIVE Number')
            if (z % 2) == 0 and z<0:
                print(z, ' Is an EVEN NEGATIVE Number')
            if (z % 2) ==1 and z<0:
                print(z, ' IS a ODD NEGATIVE Number')
            if z==0:
                print(z, 'Is is Equal to Zero')
            print('')
        play_again=input('Would you like to play again? "y" or "n" ')
        if play_again == 'y':
            continue
        if play_again == 'n':
            break
        main()
main()