0

所以我有这个定义,它基本上只是保存用户的个人资料。目前我有 1 条成功消息,我想在多个成功和错误消息之间轮换:
value = {'result': 'success', 'message': '...'}

你会怎么做?

@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'}
4

2 回答 2

2

有一个成功/错误消息列表:

import random
errors = ['dog ate it', 'flying monkeys stole it', 'rabbits attacked it']
value = {'result': 'error', 'message': random.choice(errors)}
于 2013-08-23T15:01:19.120 回答
1

您可以将消息放入数组(而不是字典)中,然后让随机索引为您选择一个:

random.randint(1,*max_number*)
于 2013-08-23T15:04:28.637 回答