python如何重复代码 [英] python how to repeat code

查看:270
本文介绍了python如何重复代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的初学者,我想重复这段代码.但是我真的不知道如何在没有"goto"的情况下做到这一点.我试图学习数小时的循环,但仍然没有成功.有任何想法吗 ?

I am a very beginner in python and I want to repeat this code. But I dont't really know how to do this without "goto". I tried to learn about loops for hours but still no sucess. Any ideas ?

import requests
addr = input()
vendor = requests.get('http://api.macvendors.com/' + addr).text
print(addr, vendor)

推荐答案

创建一个函数repeat,并在其中添加代码.然后使用while True进行无限呼叫或使用for i in range(6)进行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.

这篇关于python如何重复代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆