Make a fortune with Python

Overview

It is a program that displays a message with fortune (bad to Daikichi) when the program is executed.

Process flow

  1. Randomly decide the fortune number
  2. Get the fortune corresponding to the number from the dictionary fortunes
  3. Create a file name with a message corresponding to your fortune
  4. Read the file
  5. Get the contents of the file as a list
  6. Get elements randomly from the list

Implementation

python.qiita.rb


import random

fortunes = {
    1: 'Bad'
    2: 'Sueyoshi'
    3: 'Kokichi'
    4: 'Nakayoshi'
    5: 'Kichi'
    6: 'Daikichi'
}

#Omikuji number(1~6)Randomly determined
number = random.randint(1.6)

#Get the fortune corresponding to the number from the dictionary fortunes
fortune = fortunes[number]

#Create a read file name
file_name = 'input/fortune_' + str(number) + '.txt'

#Read the file and get it as a random one-line message
with open(file_name.encoding='utf-8') as f:
    messages = f.readline()
    message 0 random.choice(messages)

print('Your fortune...')

#Show fortune
print(fortune)

#Show message
print(message)

I will write it later

Recommended Posts

Make a fortune with Python
I made a fortune with Python.
Make a recommender system with python
Let's make a graph with python! !!
Let's make a shiritori game with Python
Let's make a voice slowly with Python
Let's make a web framework with Python! (1)
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Make a Twitter trend bot with heroku + Python
[Python] Make a game with Pyxel-Use an editor-
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
[Python] Make a simple maze game with Pyxel
Let's replace UWSC with Python (5) Let's make a Robot
Try to make a dihedral group with Python
Make Puyo Puyo AI with Python
Make a bookmarklet in Python
Create a directory with python
Make a fire with kdeplot
Make one repeating string with a Python regular expression.
Try to make a command standby tool with python
[Practice] Make a Watson app with Python! # 2 [Translation function]
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
Make a simple Slackbot with interactive button in python
[Let's play with Python] Make a household account book
Make a breakpoint on the c layer with python
Make a CSV formatting tool with Python Pandas PyInstaller
What is God? Make a simple chatbot with python
[Super easy] Let's make a LINE BOT with Python.
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Make Echolalia LINEbot with Python + heroku
Make apache log csv with python
Make a sound with Jupyter notebook
Solve ABC166 A ~ D with Python
Let's make a breakout with wxPython
Create a virtual environment with Python!
python / Make a dict from a list.
[Python] Make the function a lambda function
[Python] Generate a password with Slackbot
Solve ABC162 A ~ C with Python
Make a filter with a django template
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Let's make a supercomputer with xCAT
Make a model iterator with PySide
Make a nice graph with plotly
[Python] Inherit a class with class variables
I made a daemon with Python
Write a batch script with Python3.5 ~
Let's make a websocket client with Python. (Access token authentication)
[Practice] Make a Watson app with Python! # 3 [Natural language classification]
Associate Python Enum with a function and make it Callable
Experiment to make a self-catering PDF for Kindle with Python
[Python] Make a simple maze game with Pyxel-Make enemies appear-
Make the Python console covered with UNKO
[5th] I tried to make a certain authenticator-like tool with python