Decide who to vote for by lottery

Today is the election of the governor of Tokyo!

This year is already a ready race, and many people think that it doesn't matter who they vote for, and the turnout may drop.

https://www.amazon.co.jp/dp/4887597126 Young people lose 400,000 yen because they don't go to elections !? (Discover handbook) (Japanese) New book

There are books, but

When it comes to Only those with a support base will have an advantage, In other words, ** boring politics will continue **, so I definitely want to vote.

As you can see in the above books, if you don't have a person you want to choose, you can roll the pencil and choose it.

My goal is

Because it is.

However, there are times when there are people who do not want to choose even if there are no people who want to choose.

Give me a list of candidates and they will choose one of them We have prepared a program such as.

given_lottery.py


import secrets
import sys

def draw_lots(data=None):
    if data is None:
        return -1
    
    lot_member = data.split(',')
    r = secrets.randbelow(len(lot_member))

    return lot_member[r]

if __name__ == '__main__':
    args = sys.argv
    print(draw_lots(args[1]))

That's about it.

  1. Separate the arguments with commas
  2. Output a random value from 0 to the number of elements
  3. If you pull the list with a random value, the name will be output

It will be a movement such as.

** Addition **

In the comment, I was pointed out that there is a choice function.

That's why refactoring.

import secrets
import sys

def draw_lots(data=None):
    if data is None:
        return -1
    
    return secrets.choice(data.split(','))

if __name__ == '__main__':
    args = sys.argv
    print(draw_lots(args[1]))
  1. Separate the arguments with commas
  2. Randomly output the name

It was refreshing.

The source file is https://github.com/atworks/given-lottery I put it in.

later

image.png

> python .\given_lottery.py a,b,c,d,e
e

If you pass a string separated by commas like, randomly select one and output it.

I want to have a good voting life.

Recommended Posts

Decide who to vote for by lottery
Anxible points for those who want to introduce Ansible
For those who want to write Python with vim
Compare how to write processing for lists by language
For those who want to display images side by side as soon as possible with Python's matplotlib
Reference reference for those who want to code in Rhinoceros / Grasshopper
Problem study memo to ask for too much divided by 1000000007
[2020 version for beginners] Recommended study method for those who want to become an AI engineer by themselves