如何使程序返回到代码的顶部而不是关闭

2024-04-25 06:49:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图找出如何使Python回到代码的顶部。在SmallBasic中,您可以

start:
    textwindow.writeline("Poo")
    goto start

但我不明白你在Python中是怎么做到的:有什么想法吗?

我要循环的代码是

#Alan's Toolkit for conversions

def start() :
    print ("Welcome to the converter toolkit made by Alan.")
    op = input ("Please input what operation you wish to perform. 1 for Fahrenheit to Celsius, 2 for meters to centimetres and 3 for megabytes to gigabytes")

if op == "1":
    f1 = input ("Please enter your fahrenheit temperature: ")
    f1 = int(f1)

    a1 = (f1 - 32) / 1.8
    a1 = str(a1)

    print (a1+" celsius") 

elif op == "2":
    m1 = input ("Please input your the amount of meters you wish to convert: ")
    m1 = int(m1)
    m2 = (m1 * 100)

    m2 = str(m2)
    print (m2+" m")


if op == "3":
    mb1 = input ("Please input the amount of megabytes you want to convert")
    mb1 = int(mb1)
    mb2 = (mb1 / 1024)
    mb3 = (mb2 / 1024)

    mb3 = str(mb3)

    print (mb3+" GB")

else:
    print ("Sorry, that was an invalid command!")

start()

所以基本上,当用户完成转换时,我希望它循环回到顶部。我仍然无法将您的循环示例与此结合使用,因为每次使用def函数循环时,它都会说“op”未定义。


Tags: thetoyouforinputa1startf1
3条回答

你可以很容易地使用循环,有两种类型的循环

对于循环:

for i in range(0,5):
    print 'Hello World'

循环时:

count = 1
while count <= 5:
    print 'Hello World'
    count += 1

每个循环都会打印五次

使用无限循环:

while True:
    print('Hello world!')

这当然也适用于您的start()函数;您可以使用break退出循环,或者使用return完全退出函数,这也会终止循环:

def start():
    print ("Welcome to the converter toolkit made by Alan.")

    while True:
        op = input ("Please input what operation you wish to perform. 1 for Fahrenheit to Celsius, 2 for meters to centimetres and 3 for megabytes to gigabytes")

        if op == "1":
            f1 = input ("Please enter your fahrenheit temperature: ")
            f1 = int(f1)

            a1 = (f1 - 32) / 1.8
            a1 = str(a1)

            print (a1+" celsius") 

        elif op == "2":
            m1 = input ("Please input your the amount of meters you wish to convert: ")
            m1 = int(m1)
            m2 = (m1 * 100)

            m2 = str(m2)
            print (m2+" m")

        if op == "3":
            mb1 = input ("Please input the amount of megabytes you want to convert")
            mb1 = int(mb1)
            mb2 = (mb1 / 1024)
            mb3 = (mb2 / 1024)

            mb3 = str(mb3)

            print (mb3+" GB")

        else:
            print ("Sorry, that was an invalid command!")

如果您也要添加一个退出选项,可能是:

if op.lower() in {'q', 'quit', 'e', 'exit'}:
    print("Goodbye!")
    return

例如。

与大多数现代编程语言一样,Python不支持“goto”。相反,您必须使用控制函数。基本上有两种方法可以做到这一点。

1。循环

下面是一个示例,说明如何准确执行SmallBasic示例的功能:

while True :
    print "Poo"

就这么简单。

2。递归

def the_func() :
   print "Poo"
   the_func()

the_func()

关于递归的注意事项:只有当您有一个特定的次数想要返回到开头时(在这种情况下,当递归应该停止时添加一个case),才可以这样做。像我在上面定义的那样进行无限递归是个坏主意,因为你最终会耗尽内存!

编辑以更具体地回答问题

#Alan's Toolkit for conversions

invalid_input = True
def start() :
    print ("Welcome to the converter toolkit made by Alan.")
    op = input ("Please input what operation you wish to perform. 1 for Fahrenheit to Celsius, 2 for meters to centimetres and 3 for megabytes to gigabytes")
    if op == "1":
        #stuff
        invalid_input = False # Set to False because input was valid


    elif op == "2":
        #stuff
        invalid_input = False # Set to False because input was valid
    elif op == "3": # you still have this as "if"; I would recommend keeping it as elif
        #stuff
        invalid_input = False # Set to False because input was valid
    else:
        print ("Sorry, that was an invalid command!")

while invalid_input : # this will loop until invalid_input is set to be True
    start()

相关问题 更多 >