python how to repeat code

前端 未结 3 1031
灰色年华
灰色年华 2020-12-20 10:56

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 n

3条回答
  •  执笔经年
    2020-12-20 11:10

    A loop is the best way to achieve this. For example check out this pseudo code:

    // While person is hungry
    // Eat food a bite of food
    // Increase amount of food in stomach
    // If amount of food ate fills stomach
    // person is no longer hungry
    // stop eating food
    

    In code this would look something like this:

    food_in_stomach = 0
    
    while food_in_stomach <= 8:
      eat_bite_of_food()
      food_in_stomach = food_in_stomach + 1
    

    You could therefore implement your code like the following:

    times_to_repeat = 3
    
    while times_to_repeat >= 0:
      addr = input()
      vendor = requests.get('http://api.macvendors.com/' + addr).text
      print(addr, vendor)
      times_to_repeat -= 1
    

提交回复
热议问题