I made a fortune with Python.

Introduction

The Advent calendar was free, so I'll write a little bit about Python. I usually make web apps using Flask, but I felt that I didn't understand Python itself probably because I relied on the framework. So, to practice Python, I first made an Omikuji. (Currently, Flask's Advent calendar is empty. If you know Flask even a little, please join us!)

environment

Ubuntu18.04LTS Python3.6.9 vim

code

This time I made an Omikuji like this.

kuji.py


from random import choice


play = input('You can draw a fortune by typing play.: ')

while True:
    if play == 'play':
        break

    print('Type it again.')
    play = input('You can draw a fortune by typing play.: ')

while True:
    KUJI = ['Daikichi', 'Nakayoshi', 'Kokichi', 'Sueyoshi', 'Bad', '大Bad']
    print(choice(KUJI))
    continue_ = input('Would you like to pull it again?[y/n]: ')
    while True:
        if continue_ != 'y':
            if continue_ != 'n':
                input('Enter y or n.: ')
            else:
                break
        else:
            break

    if continue_ == 'y':
        pass
    else:
        break

print('Finished.')

It's boring if it's an ordinary Omikuji, so I tried a little ingenuity. If it's really just an Omikuji, it will end in two lines. Like this.

from random import choice
print(choice(['Daikichi', 'Nakayoshi', 'Kokichi', 'Sueyoshi', 'Bad', '大Bad']))

Postscript

I devised a little more by referring to the advice in the comments.

kuji2.py


from random import choices #choice → choices


play = input('You can draw a fortune by typing play.: ')

while True:
    if play == 'play':
        break

    print('Type it again.')
    play = input('You can draw a fortune by typing play.: ')

while True:
    KUJI = ['Daikichi', 'Nakayoshi', 'Kokichi', 'Sueyoshi', 'Bad', '大Bad']
    print(choices(KUJI, weights=[1, 5, 10, 10, 5, 1])[0]) #I changed here.
    continue_ = input('Would you like to pull it again?[y/n]: ')
    while True:
        if continue_ == 'y' or continue_ == 'n': #I have also prepared the code here.
            break
        else:
            continue_ = input('Enter y or n.: ')

    if continue_ == 'n':
        break

print('Finished.')

that's all.

Recommended Posts

I made a fortune with Python.
I made a daemon with Python
I made a character counter with Python
I made a Hex map with Python
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
I made blackjack with python!
Make a fortune with Python
I made a python text
I made blackjack with Python.
I made wordcloud with Python.
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
[Python] I made a Youtube Downloader with Tkinter.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a Twitter BOT with GAE (python) (with a reference)
I made a Line-bot using Python!
I made a net news notification app with Python
I made a Python3 environment on Ubuntu with direnv.
I made a LINE BOT with Python and Heroku
I made a simple typing game with tkinter in Python
I made a package to filter time series with python
I made a simple book application with python + Flask ~ Introduction ~
I made a puzzle game (like) with Tkinter in Python
I made a payroll program in Python!
I drew a heatmap with seaborn [Python]
I tried a functional language with Python
What I did with a Python array
I made a life game with Numpy
I made a stamp generator with GAN
After studying Python3, I made a Slackbot
I made a WEB application with Django
〇✕ I made a game
I made a simple circuit with Python (AND, OR, NOR, etc.)
I made a library to easily read config files with Python
I made a package that can compare morphological analyzers with Python
I made a Nyanko tweet form with Python, Flask and Heroku
I made a lot of files for RDP connection with Python
[Python] I made an image viewer with a simple sorting function.
I made a shuffle that can be reset (reverted) with Python
I made a poker game server chat-holdem using websocket with python
I made a segment tree with python, so I will introduce it
I made a stamp substitute bot with line
I want to make a game with Python
Procedure for creating a LineBot made with Python
I made a simple Bitcoin wallet with pycoin
I made a LINE Bot with Serverless Framework!
I made a random number graph with Numpy
I want to write to a file with Python
I made a Caesar cryptographic program in Python.
I made a Python Qiita API wrapper "qiipy"
I made a QR code image with CuteR
I made a tool to automatically browse multiple sites with Selenium (Python)
I tried to discriminate a 6-digit number with a number discrimination application made with python
I made a tool that makes decompression a little easier with CLI (Python3)
I made a module PyNanaco that can charge nanaco credit with python