简体   繁体   English

如何在Python /金字塔中使用多个随机字符串?

[英]How to use multiple random strings in Python / Pyramid?

So I have this definition which basically just saves a user's profile. 所以我有了这个定义,基本上只保存了用户的个人资料。 Currently I have 1 success message, I would like to rotate between multiple success and error messages the: 当前我有1条成功消息,我想在多个成功消息和错误消息之间轮换:
value = {'result': 'success', 'message': '...'}

How would you go about this? 您将如何处理?

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

Have a list of success/error messages: 列出成功/错误消息:

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

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

random.randint(1,*max_number*)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM