有没有一种方法可以在每次执行for循环时更改变量?

时间:2019-01-16 21:08:14

标签: python for-loop

我不知道如何使用for循环为列表中的每个新名称创建一个新变量。

作为这门编码的新手,我正在做一个小学校项目,试图缩短我的代码,但遇到了一个很长的问题。因此,我尝试将其放入for循环中,以便对于列表中的每个不同名称(奇怪的给定狗名)我都可以为其获取一个新变量,但只会卡住。无论如何,有人可以提出解决方案吗?

import random

class info():

    def __init__(self, name, exercise, intel, friend, drool):
        self.name = name
        self.exercise = exercise
        self.intel = intel
        self.friend = friend
        self.drool = drool


    def __repr__(self):
        return "\nName : {}\na-Exercise : {}\nb-Intelligence : {}\nc-Friendliness : {}\nd-Drool : {}".format(
            self.name, self.exercise, self.intel, self.friend, self.drool)

dog_names = ["Annie the Afgan Hound", "Bertie the Boxer", "Betty the Borzoi", "Charlie the Chihuahua", "Chaz the Cocker Spaniel", "Donald the Dalmatian", "Dottie the Doberman", "Fern the Fox Terrier", "Frank the French Bulldog", "George the Great Dane", "Gertie the Greyhound", "Harry the Harrier", "Ian the Irish Wolfhound", "Juno the Jack Russell", "Keith the Kerry Blue", "Larry the Labrador", "Marge the Maltese", "Max the Mutt", "Nutty the Newfoundland", "Olive the Old English Sheepdog", "Peter the Pug", "Poppy the Pekingese", "Rosie the Rottweiler", "Ruby the Retriever", "Sam the Springer Spaniel", "Sukie the Saluki", "Vernon the Vizsla", "Whilma the West Highland Terrier", "William the Whippet", "Yolande the Yorkshire Terrier"]

#This is what i want to shorten

a = info("Annie the Afgan Hound", random.randint(1, 5), random.randint(1, 100), random.randint(1, 10), random.randint(1, 10))#1

b = info("Bertie the Boxer", random.randint(1, 5), random.randint(1, 100), random.randint(1, 10), random.randint(1, 10))
#etc

我想为每个名字创建一个“卡片”;这样,每次运行此代码时,每个名称都有一个新变量。例如...

a = Name:Annie the Afgan Hound |
    Exersice:10 |
    Intelligence:34 |
    Friendliness:4 |
    Drool:2

b = Name:Bertie the Boxer |
    Exersice:7 |
    Intelligence:87 |
    Friendliness:9 |
    Drool:10

这是新手,所以将不胜感激:)

2 个答案:

答案 0 :(得分:1)

您需要这样的东西:

LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
  switch (Message) 
  {
    case WM_USER_SHELLICON: 
    {
      switch(LOWORD(lParam)) 
      {   
        case WM_RBUTTONDOWN: 
        {
          HMENU hPopupsMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDC_POPUPS));
          if (hPopupsMenu)
          {
            HMENU hTrayPopupMenu = GetSubMenu(hPopupsMenu, 0);
            if (hTrayPopupMenu) 
            {
              POINT lpClickPoint; 
              GetCursorPos(&lpClickPoint);         
              SetForegroundWindow(hWnd);
              TrackPopupMenu(hTrayPopupMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_BOTTOMALIGN, lpClickPoint.x, lpClickPoint.y, 0, hWnd, NULL);
            }
            DestroyMenu(hPopupsMenu);
          }
        } 
        break;
      }
      break;
    }
...

如果这是一项家庭作业,那么您可能不希望我将其全部写下来。

答案 1 :(得分:0)

您几乎肯定会希望将它们放入列表:

kennel = []
for name in dog_names:
    kennel.append(info(name,
                       random.randint(1, 5), 
                       random.randint(1, 100),
                       random.randint(1, 10),
                       random.randint(1, 10)))

完成此循环后,将在列表中包含所有狗info对象,并且可以方便地遍历该列表。

for dog in kennel:
    ...