gpt4 book ai didi

python - 如何让程序回到代码的顶部而不是关闭

转载 作者:太空狗 更新时间:2023-10-29 19:32:59 25 4
gpt4 key购买 nike

<分区>

我正在尝试弄清楚如何让 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”未定义。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com