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!)
Ubuntu18.04LTS Python3.6.9 vim
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']))
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