In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to achieve the blind box lottery function in python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Case name: blind box lottery
Case description:
There are three prizes in a competition, which are the first prize, the second prize and the third prize.
There are three choices for the first prize: Rice cooker, air conditioner and washing machine.
There are three choices for the second prize: sneakers, watches and skateboards.
There are three kinds of prizes for the third prize: couplets, handbags and calcium tablets
Now you need to write a function that sets a parameter that is used to receive the string of the corresponding award.
That is, if the parameter value is equal to the first prize, any one of the selectable prizes under the corresponding prize is returned, so as to achieve the effect of blind box extraction.
If the string of the parameter value is not the first prize, the second prize or the third prize, the direct return input is incorrect and there is no word for the award. For example, if the parameter value is equal to the fourth prize, the direct return input is incorrect and there is no such award.
When the awards are drawn, the corresponding inventory will be reduced by one. If the prize inventory is 00:00, there will be no prizes.
The code is implemented as follows:
# guess Blind Box import random# stores all prize information in the form of a dictionary nested dictionary w = {'first prize': {'rice cooker': 2, 'air conditioner': 1, 'washing machine': 1}, 'second prize': {'sneakers': 2, 'watch': 5, 'skateboard': 3} Third prize: {'couplet': 4, 'handbag': 2, 'calcium tablet': 3}} def lottery_plus (level=None): if level= = 'first prize': w_num = w.get ('first prize') # inventory dictionary {'rice cooker': 2, 'air conditioning': 1 Washing machine: 1} jackpot = [] # prize pool: whose stock is not zero into the prize pool ['rice cooker', 'air conditioner', 'washing machine'] # whose inventory is not zero into the prize pool for k in w_num.keys (): # ergodic ['rice cooker', 'air conditioner' 'washing machine'] if w_num.get (k)! = 0: # if the stock is not 0 jackpot.append (k) # enter the prize pool [] .append ('rice cooker') try: result = random.choice (jackpot) # randomly pick a prize # inventory-1 w.get ('one First prize') [result]-= 1 print ('first prize pool:' End='') print (jackpot) return 'Congratulations on winning the first prize' + result except: return'I'm sorry The first prize will be wrong when the'# random.choice ([]) is passed into the empty list. If the list is empty, all the prizes have been taken away. All prizes inventory is zero elif level = = 'second prize': w_num = w.get ('second prize') jackpot = [] # whose inventory is not zero into the prize pool for k in w_num.keys (): if w_num.get (k)! = 0: jackpot.append (k) Try: result = random.choice (jackpot) # inventory-1 w.get ('second prize') [result]-= 1 print ('second prize pool:' End='') print (jackpot) return 'Congratulations on winning the second prize' + result except: return'I'm sorry The second prize does not have 'elif level =' third prize': w_num = w.get ('third prize') jackpot = [] # whose inventory is not zero into the prize pool for k in w_num.keys (): if w_num.get (k)! = 0: jackpot.append (k) Try: result = random.choice (jackpot) # inventory-1 w.get ('third prize') [result]-= 1 print ('third prize pool:' End='') print (jackpot) return 'Congratulations on winning the third prize' + result except: return'I'm sorry The third prize is gone. 'else: return' did not win the prize.
Here is the result of calling the function:
This is the end of the article on "how to realize the blind box lottery function in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.