中午吃什么是个老问题,闲暇时间写个代码来解决。
代码可以自行修改 权重 数
#coding='utf-8' import random import collections as coll import sys from PyQt4 import QtGui class Choose(QtGui.QMainWindow): #数字为权重数字,修改即可 data = {"老盛昌":10, "羊汤烩面":10, "江南农庄":4, "辛香汇":4, "页品盛酸菜鱼": 3, "青花椒鱼片":3, "pizza":2, "麦当劳":2} #第一种 根据元素权重值 "A"*2 ..等,把每个元素取权重个元素放到一个数组中,然后最数组下标取随机数得到权重 def eat_something(self): all_data = [] for v, w in self.data.items(): temp = [] for i in range(w): temp.append(v) all_data.extend(temp) # print("初始化数据 = ", all_data) random.shuffle(all_data) # print("打乱数据后 = ", all_data) print("开始摇奖...^_^") sum = 0 for i in range(1, 5): n = random.randint(0, len(all_data) - 1) print("n is", n) sum += n # print("sum is", sum) n = int(sum / 4) # print("n average is", n) button1 = QtGui.QMessageBox.about(self, '甭纠结', '中午就吃【' + all_data[n] + '】') # print("甭纠结,中午吃...", all_data[n]) if __name__ == '__main__': app = QtGui.QApplication(sys.argv) c = Choose() c.eat_something() sys.exit(app.exec_())
运行效果:
文章的脚注信息由WordPress的wp-posturl插件自动生成