How to make program go back to the top of the code instead of closing
我正在努力想办法让Python回到代码的顶端。在smallbasic中,您需要
1 2 3 | start: textwindow.writeline("Poo") goto start |
但是我不知道你是怎么用python来做的:有什么想法吗?
我要循环的代码是
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 27 28 29 30 31 32 33 34 35 36 37 38 | #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"没有定义。
使用无限循环:
1 2 | while True: print('Hello world!') |
这当然也适用于您的
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 27 28 29 30 31 32 33 34 35 | 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!") |
如果要添加退出选项,也可以是:
1 2 3 | if op.lower() in {'q', 'quit', 'e', 'exit'}: print("Goodbye!") return |
例如。
与大多数现代编程语言一样,python不支持"goto"。相反,您必须使用控制功能。基本上有两种方法可以做到这一点。
1。循环
下面是一个示例,说明如何精确执行smallbasic示例所执行的操作:
1 2 | while True : print"Poo" |
就这么简单。
2。递归
1 2 3 4 5 | def the_func() : print"Poo" the_func() the_func() |
关于递归的注意事项:只有当你有一个特定的次数想要回到开始的时候才这样做(在这种情况下,在递归应该停止的时候添加一个案例)。像我上面定义的那样执行无限递归是一个坏主意,因为您最终会耗尽内存!
编辑以更具体地回答问题1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #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() |
你可以很容易地使用循环,有两种类型的循环
for循环:
1 2 | for i in range(0,5): print 'Hello World' |
循环时:
1 2 3 4 | count = 1 while count <= 5: print 'Hello World' count += 1 |
每个循环打印"你好,世界"五次
python有控制流语句,而不是
如果决定运行以下示例代码,请小心。如果您想终止进程,请在shell运行时按control+c。请注意,该进程必须在前台才能工作。
1 2 3 | while True: # do stuff here pass |
行
现在假设您想反复要求用户永远输入,并且只在用户输入字符"q"表示退出时退出程序。
你可以这样做:
1 2 3 4 | while True: cmd = raw_input('Do you want to quit? Enter \'q\'!') if cmd == 'q': break |
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | def start(): Offset = 5 def getMode(): while True: print('Do you wish to encrypt or decrypt a message?') mode = input().lower() if mode in 'encrypt e decrypt d'.split(): return mode else: print('Please be sensible try just the lower case') def getMessage(): print('Enter your message wanted to :') return input() def getKey(): key = 0 while True: print('Enter the key number (1-%s)' % (Offset)) key = int(input()) if (key >= 1 and key <= Offset): return key def getTranslatedMessage(mode, message, key): if mode[0] == 'd': key = -key translated = '' for symbol in message: if symbol.isalpha(): num = ord(symbol) num += key if symbol.isupper(): if num > ord('Z'): num -= 26 elif num < ord('A'): num += 26 elif symbol.islower(): if num > ord('z'): num -= 26 elif num < ord('a'): num += 26 translated += chr(num) else: translated += symbol return translated mode = getMode() message = getMessage() key = getKey() print('Your translated text is:') print(getTranslatedMessage(mode, message, key)) if op.lower() in {'q', 'quit', 'e', 'exit'}: print("Goodbye!") return |
您需要使用while循环。如果你做了一个while循环,并且在循环之后没有指令,那么它将成为一个无限循环,直到你手动停止它,它才会停止。
编写for或while循环并将所有代码放入其中?Goto类型编程已经成为过去。
https://wiki.python.org/moin/forloop网站