2013-08-23 88 views
0

所以我有這個定義,基本上只保存用戶的個人資料。 目前我有1個成功消息,我想在多個成功和錯誤消息之間旋轉:
value = {'result': 'success', 'message': '...'}如何在Python/Pyramid中使用多個隨機字符串?

你會怎麼做呢?

@view_config(route_name="profile", request_method='POST') 
def save_profile(self): 
    try: 
     json = self.request.json_body 
     first_name = str(json['firstName']) 
     last_name = str(json['lastName']) 
     organization = str(json['organization']) 
     title = str(json['title']) 
     phones = (json['phones']) 
     emails = (json['emails']) 
     self.profiles.update(firstName=first_name, lastName=last_name, organization=organization, title=title, emails=emails, phones=phones) 
     value = {'result': 'success', 'message': "Saved! Worry not, nothing sent to the NSA... as far as we know"} 
    except Exception, err: 
     print err 
     value = {'result': 'error', 'message': 'The internets are clogged up, our monkeys are checking it out'} 
+2

如何將消息粘貼到列表中,然後從該列表中隨機選擇?你也不需要'(json ['phones'])'和類似的括號。 –

回答

2

有成功/錯誤消息的列表:

import random 
errors = ['dog ate it', 'flying monkeys stole it', 'rabbits attacked it'] 
value = {'result': 'error', 'message': random.choice(errors)} 
+0

啊謝謝:D現在不想讓用戶嘿嘿... –

1

你可以把你的信息在陣列(而非字典),然後讓一個隨機指數選擇一個適合你:

random.randint(1,*max_number*) 
相關問題