代码这这里: http://pastebin.com/N30GhaJS
![]() | 1 Ziya 2016-07-20 16:05:55 +08:00 把 post 的内容建个字典,每次 if 的时候修改一下要改变的值 |
![]() | 2 wlsnx 2016-07-20 16:07:03 +08:00 'subject':title[get_int].encode(encoding='gbk'), |
3 testpachong OP @wlsnx 这个跟我指定有什么区别吗? |
4 testpachong OP @Ziya 给个代码吧,没什么经验 |
![]() | 5 wlsnx 2016-07-20 17:18:14 +08:00 @testpachong 你不是要精简代码吗 |
6 testpachong OP @wlsnx ...。。。我还需要他能像上面代码那样运行啊 |
![]() | 7 mgna17 2016-07-20 17:29:04 +08:00 也许是要这样么 post = { 'a': 1, 'b': 2} if condition0: post.update(c=3) if condition1: post.update(d=4) |
8 lxy 2016-07-20 17:32:13 +08:00 看起来像是刚开始学习编程的…… |
![]() | 9 Leafove 2016-07-20 17:47:44 +08:00 一般这像是学校里老是举的反面例子... |
![]() | 10 Leafove 2016-07-20 17:48:05 +08:00 打错字了..抱歉 |
![]() | 11 xujinkai 2016-07-20 20:26:50 +08:00 如果变化的只是 title 和 content ,可以直接 title[get_int]和 content[get_int]; 还有一种方法是写一个 post_list = {0: {...}, 1: {...}, ...},然后 post = post_list[get_int]; 总之大段的 if else 不是个好主意 |
12 vimffs 2016-07-21 10:55:31 +08:00 其实,@wlsnx 已经指出了,只是 OP 没看懂? 给出的代码可用下面的替换: ``` get_int = random.randint(0, 3) post = { 'formhash':formhash_code, 'wysiwyg':'0', 'iconid':'', 'updateswfattach':'0', 'subject':title[get_int].encode(encoding='gbk'), 'checkbox':'0', 'message':content[get_int].encode(encoding='gbk'), 'attachdesc[]':'', 'localid[]':'', 'attachperm[]':'0', 'attachprice[]':'0', 'attachdesc[]':'', 'localid[]':'1', 'attachperm[]':'0', 'attachprice[]':'0', 'readperm':'', 'price':'', } ``` |
![]() | 13 wlsnx 2016-07-21 12:01:23 +08:00 if get_int ==0 or get_int : 这句可以去掉 如果 get_int 是个数字, get_int ==0 or get_int 永远是 True 数字 x 的 __nonzero__ 为 True 的条件是 x != 0 if get_int ==0 or get_int 相当于 if get_int == 0 or get_int != 0 |
14 testpachong OP @wlsnx 我需要让他永远是 true ,我只给了部分代码,不是完整代码。 总之谢谢。 |