2013-11-24 126 views
-2

好的,我有一個有10個對象的類,它們具有屬性self.planet,self.distance,self.distsquared,self.radius,self.diamater where distance/distsquared/radius /直徑都是整數。我想在用戶搜索一個行星名稱,然後更改其中一個屬性的功能。根據用戶輸入更改屬性

例如,用戶應該輸入名稱「Jupiter」,然後找到該對象,該函數的下一行將要求用戶向屬性self.distance添加一定的總和。

目前第一類是設置如下:

class Planets(): 
     def __init__(self, planetName, dist, radius, diameter): 
       self.planetName= planetName 
       self.dist= dist 
       self.radius= radius 
       self.diameter= diameter 

這然後通過planetObjects=[Planets(*p) for p in planetList]這是對象的名單,我想變成一本字典,以便用戶可以搜索planetName檢索,並改變距離

一些用戶建議我使用這個字典,但我不知道如何去做這件事。目前,我的課程將列表轉換爲對象列表,這些對象具有這些屬性,用戶應該可以通過搜索Planet名稱來更改這些屬性,然後更改其中的一個屬性。

類是目前只是一個簡單的類,它有一個構造函數和一個__str__功能

含義,功能啓動時,會詢問用戶像「你想哪個星球來改變?」,用戶輸入「木星「該程序問道,」與木星的距離有何變化?「用戶添加例如450左右。

我現在的代碼是一個打開infile並將其變成列表清單的函數。這個列表然後變成對象。我將它轉換爲對象,可以輕鬆地對它進行排序並根據以前的值添加新值。但在這一點上,用戶還必須能夠通過搜索行星名稱然後更改其中一個屬性來改變值 - 這就是我迷失方向並需要幫助的地方!

有沒有辦法做到這一點?提前致謝!

+2

你能發表一些你爲此寫的代碼嗎? – Sikorski

+0

爲了更好地理解數據類型,您可以閱讀[python教程](http://docs.python.org/3/tutorial/datastructures.html#dictionaries)中的字典 –

+0

我建議避免重複信息,例如只存儲一個半徑和直徑,因爲他們基本上是相同的信息與他們之間的轉換,* 2在這種情況下,相同的dist和squared_dist有一個簡單的轉換** 2因此不需要存儲兩個你總是可以訪問爲您完成轉換。 –

回答

1

在僞代碼:

class Planet(object): 
    # Define your planet class here 
    # any attributes that you do NOT need the user to be able to edit should start with _ 

Planets = [Planet('Mercury'..... 
#or better 
PlanetDict = {'Mercury':Planet(.... 

which = PromptUserForPlanet() 

p = PlanetDict.get(which) # or find and return it if you didn't use a dictionary 

for att in dir(p): 
    if not att.startswith('_'): 
     input = raw_input('%s: (%s)' % (attr, repr(getattr(p,attr))) 
     if len(input) > 0: 
     setattr(p,att,input) # You may wish to do some type conversion first 

由於p是到字典條目,將更改主要對象的引用。

+0

非常感謝。但我不太明白,我有一個名爲planetObjects的對象列表,它是由一系列列表組成的。所以基本上我必須把對象列表變成一個字典(怎麼樣?),然後通過一些函數來訪問它? 非常感謝您的幫助! – user2982959

+0

@ user2982959'planetObjects'中有什麼樣的對象?字符串,自定義類對象,數組? –

+0

它只是一堆對象,它們都具有設置爲字符串的第一個屬性,其他屬性都是整數。 – user2982959

1

鑑於你的班級Planets,這可能會像這樣解決。我假設planetList的結構像這個代碼一樣。如果不是,您可能需要稍微修改代碼。

def increment_dist(planets): 
    name = raw_input('Please enter planet name') 
    try: 
     planets[name].dist += int(raw_input('Increment distance by (integer)')) 
    except KeyError: 
     print('No planet called {}'.format(name)) 
    except ValueError: 
     print('That is not an integer') 

planetList = [('Tellus', 1, 2, 4), ('Mars', 1, 3, 9)] 
planet_dict = {name: Planets(name, dist, radius, diameter) for 
       name, dist, radius, diameter in planetList} 

increment_dist(planet_dict) 
+0

這是有點的我如何建立我的宇宙。 唯一的問題是,用戶必須能夠搜索一個特定的星球,說特力,並增加了一些距離整數。因此,主窗口要求用戶搜索一個行星,然後要求用戶添加一個數字,這會改變行星的距離,而不會改變其他變量。 – user2982959

+0

對不起,最後一個問題。如果我已經有了一個對象列表並且想把它們全部轉換成字典類。我怎樣才能輕鬆做到這一點?當我剛剛做了Universe類的前兩種方法,並嘗試打印字典時,它會顯示「無」,就好像字典是空的。 – user2982959

+0

類行星(): DEF __init __(個體,planetName,DIST,半徑,直徑): self.planetName = planetName self.dist = DIST self.radius =半徑 self.diameter =直徑 那麼這是通過planetObjects = [行星(* p),用於在planetList p]檢索 這是對象列表,我想變成一個詞典,因此用戶可以搜索planetName,並改變距離 – user2982959