見出し画像

[Python]誕生日当てゲームをスクリプトと楽しむ

1.誕生日当てゲーム

ユーザーとスクリプトの対話から、最後にスクリプトがユーザーの誕生日を当てるゲームを作成します。プログラム実行時にユーザーの名前を引数に渡します。

2.コード

from sys import argv

script_name, user_name = argv

script_name = script_name.replace(".py", "")
cp_prompt=f"{script_name}:"
user_prompt=f"{user_name}:"

def calc_birth_date(num):
    birth_date = num - 225
    if len(str(birth_date)) == 3:
        month = str(birth_date)[0]
        month = month.zfill(2)
        day = str(birth_date)[1:]
        return [month, day]
    elif len(str(birth_date)) == 4:
        month = str(birth_date)[:2]
        day = str(birth_date)[2:]
        return [month, day]

print(f"{cp_prompt}こんにちは、{user_name}さん")
print(f"{cp_prompt}あなたの誕生日を当てましょう!")
print(f"{cp_prompt}あなたの生まれた月を4倍してください(メモにこちらをどうぞ)")
month_4times = int(input(user_prompt))
print(f"{cp_prompt}さらにそれを9足してくださいください(メモにこちらをどうぞ)")
month_4times_add9 = int(input(user_prompt))
print(f"{cp_prompt}さらにそれを25倍してくださいください(メモにこちらをどうぞ)")
month_4times_add9_25times = int(input(user_prompt))
print(f"{cp_prompt}さらにそれに生まれた日を足してください(メモにこちらをどうぞ)")
month_4times_add9_25times_addays = int(input(user_prompt))
print(f"{cp_prompt}ヽ(。_。)ノハッ!天啓がおりました")
birth_list = calc_birth_date(month_4times_add9_25times_addays)
birth_date = "/".join(birth_list)
print(f"{cp_prompt}ズバリあなたの誕生日は{birth_date}ですね!")

3.実行結果

caster:こんにちは、nobiさん
caster:あなたの誕生日を当てましょう!
caster:あなたの生まれた月を4倍してください(メモにこちらをどうぞ)
nobi:32
caster:さらにそれを9足してくださいください(メモにこちらをどうぞ)
nobi:41
caster:さらにそれを25倍してくださいください(メモにこちらをどうぞ)
nobi:1025
caster:さらにそれに生まれた日を足してください(メモにこちらをどうぞ)
nobi:1032
caster:ヽ(。_。)ノハッ!天啓がおりました
caster:ズバリあなたの誕生日は08/07ですね!

この記事が気に入ったらサポートをしてみませんか?