本文介绍了如何编写循环以重复代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Python的初学者,我想重复这段代码.但是我真的不知道如何在没有"goto"的情况下做到这一点.我试图学习循环,但不了解如何应用循环.
I am a very beginner in Python and I want to repeat this code. But I don't really know how to do this without "goto". I tried to learn about loops but did not understand how to apply them.
import requests
addr = input()
vendor = requests.get('http://api.macvendors.com/' + addr).text
print(addr, vendor)
推荐答案
创建一个函数 repeat
并在其中添加代码.然后使用 while True
进行无限调用,或者使用 for range(6)中的i进行6次调用`:
Create a function repeat
and add your code in it. Then use while True
for infinite call or for i in range(6)
for 6 times call`:
import requests
def repeat():
addr = input()
vendor = requests.get('http://api.macvendors.com/' + addr).text
print(addr, vendor)
while True:
repeat()
请注意,不建议以任何语言使用goto,并且在python中不可用.这会引起很多问题.
Note that goto is not recommended in any language and is not available in python. It causes a lot of problems.
这篇关于如何编写循环以重复代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!