首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使程序返回到代码的顶部而不是关闭

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

Stack Overflow用户
提问于 2013-09-13 17:20:36
回答 7查看 385.4K关注 0票数 13

我正在努力弄清楚如何使Python回到代码的顶端。在SmallBasic中,你需要

代码语言:javascript
复制
start:
    textwindow.writeline("Poo")
    goto start

但我不知道你是如何在Python中做到这一点的:/有人有什么想法吗?

我想循环的代码是

代码语言:javascript
复制
#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“。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2013-09-13 17:24:25

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

1.循环

关于如何准确地执行SmallBasic示例的示例,如下所示:

代码语言:javascript
复制
while True :
    print "Poo"

就这么简单。

2.递归

代码语言:javascript
复制
def the_func() :
   print "Poo"
   the_func()

the_func()

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

编辑以更具体地回答问题

代码语言:javascript
复制
#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 False
    start()
票数 10
EN

Stack Overflow用户

发布于 2013-09-13 17:22:39

使用无限循环:

代码语言:javascript
复制
while True:
    print('Hello world!')

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

代码语言:javascript
复制
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!")

如果要添加一个退出选项,可以如下所示:

代码语言:javascript
复制
if op.lower() in {'q', 'quit', 'e', 'exit'}:
    print("Goodbye!")
    return

例如。

票数 19
EN

Stack Overflow用户

发布于 2013-09-13 17:27:06

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

For循环:

代码语言:javascript
复制
for i in range(0,5):
    print 'Hello World'

循环:

代码语言:javascript
复制
count = 1
while count <= 5:
    print 'Hello World'
    count += 1

每个循环打印“”五次

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18791882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档