[PYTHON] Various fortune games

Introduction

Using python, you can create fortunes by various methods such as random, if, else, and random numbers. This time it was the final product, so I decided to make an Omikuji in various ways.

The simplest fortune

omikuzi.py


import random

destiny = ['Daikichi','Nakayoshi','Kokichi','Kichi','Bad','大Bad']

print(random.choice(destiny))

This made a fortune using random. By using random, you can create a program that randomly selects from the list. ** As a demerit, it is not possible to reproduce the rarity of Daikichi and Daikoku. Daikichi comes out easily and it's not interesting **

Omikuji that can manipulate the probability

omikuzi.py


print("1~Please enter a number up to 60")

number = input()

if 0 <= int(number) <= 3:
    print("Daikichi")

elif 4 <= int(number) <= 10:
    print("Nakayoshi")

elif 11 <= int(number) <= 20:
    print("Kokichi")

elif 21 <= int(number) <= 40:
    print("Kichi")

elif 41 <= int(number) <= 55: 
    print("Bad")

elif 56 <= int(number) <= 60:
    print("Great villain")

else:
    print("die")

The if used here means that if the conditional expression that follows it is satisfied, the processing that follows it is performed. elif is an abbreviation for else if. else processes else when if does not exist. Based on these facts, conditional branching was performed. ** By using this method, the probability of appearance of Daikichi and the villain can be extremely low, and the probability of appearance of things in the middle such as Kokichi can be increased. ** **

tkinter fortune

omikuzi.py



import tkinter.messagebox as mb
import tkinter as tk
import random
      

a = tk.Tk()
a.title("Omikuji game")
a.geometry("400x250")


b = tk.Label(text="Click")
b.pack()

kekka = [["Daikichi"], ["Buy the lottery ticket now! !!"], ["Kichi", "There is a good thing!"],["小Kichi", "Today's dinner is my favorite!"],
["Bad", "Today is a day of continuous failure"],["大Bad", "Don't go outside today. I do not know what will happen"]
]    
def botan():
    c = random.randrange(len(kekka))
    r = kekka
    mb.showinfo(r[0], r[1])

bota = tk.Button(text ="Draw fortune",width =40, height = 10, command = botan)
bota.pack()

a.mainloop()

I created a random fortune by using random numbers. I prepared multiple results after pressing the button.

Execution result

2020-07-08 (1).png 2020-07-08 (2).png

Impressions

I referred to the article, so it went smoothly. Personally, I think the Omikuji, which allows you to decide the probability yourself, is the best. Next time I wanted to make it from scratch.

Referenced articles

Omikuji made with Tkinter + α Introduction to how to use Pythontkinter Let's make an Omikuji app

Recommended Posts

Various fortune games
Various scraping