H5W3
当前位置:H5W3 > 其他技术问题 > 正文

想请教一个关于for的问题

为什么在第一个for循环里可以列出所有的元素,但在第二个for循环里只能列出第一个元素呢?

# -*- coding: utf-8 -*-
#from collections import OrderedDict
import re
class inventory(object):
    def __init__(self):
        self.inventory_list = []
        #self._index = 1
    def inventory_add(self, name, item):
        self.inventory_list.append((name, item))
        #self._index += 1
    def inventory_del(self, name, item):
        self.inventory_list.remove((name, item))
    def print_inventory(self):
        print u'你拥有:'
        for index, value in enumerate(self.inventory_list, 1):            
            print index, '.', value[0]
        return self._judgement()        
    
    def _judgement(self):
        print u'输入0返回选择场景,输入具体数字显示详细描述。'
        self._choice = raw_input(">" )
        #print self.inventory_list
        #if re.match(r'[0-9]+', self._choice):
        #self._ok_choice = int(self._choice)
            #if self._ok_choice == 0:
                #self.print_inventory()
            #else:
        for key, name in enumerate(self.inventory_list, 1):
            print key, name[0]
            if int(self._choice) == key:
                print key
                self.a = name[1]
                self.a.printself()
                self.print_inventory()
                return self._judgement()
            else:
                print 'Wrong!'
                return self._judgement()
        #else:
            #print 'Wrong!'
            #return self._judgement()

class mobilephone(object):
    description = u'一部手机,话费和电量应该充足吧'
    def printself(self):
        print self.description
        
class money(object):
    description = u'30元'
    def printself(self):
        print self.description
        
class student_card(object):
    description = u'余额还够吧?'
    def printself(self):
        print self.description        
        
inventory = inventory()
inventory.inventory_add(u'手机', mobilephone())
inventory.inventory_add(u'钱', money())
inventory.inventory_add(u'学生卡', student_card())
print inventory.inventory_list
inventory.print_inventory()

本文地址:H5W3 » 想请教一个关于for的问题

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址