【Python学習用アプリ(コピペ可)】計算ゲーム

※本サイトにはプロモーション・広告が含まれています。

Yuの自作アプリをご紹介する記事となります

今回は、

計算ゲーム

になります

前回同様、ターミナルもしくはコマンドプロンプト上で行うゲームですので、

見た目については言わないでください!笑

40秒ほどのゲーム紹介のムービーをご覧いただき、概要を掴んだら、

「コード」へ進み、コピペで実行してみてください!

Pythonのインストールが済んでない!

Pythonファイルを実行ってどうやるの?

っていう方は以前の記事をご参考にインストール等の準備を進めてください

自作アプリの紹介第一弾「じゃんけんゲーム」記事より

筆者プロフィール

筆者プロフィールアイコン

【現職】プロダクトマネージャー

【副業】ブログ(月間17万PV)/YouTube/Web・アプリ制作

「プログラミング × ライティング × 営業」の経験を活かし、30後半からのIT系職へシフト。当サイトでは、実際に手を動かせるWebアプリの開発を通じて、プログラミングはもちろん、IT職に必要な情報を提供していきます。

【当ブログで紹介しているサイト】

当サイトチュートリアルで作成したデモ版日報アプリ

Django × Reactで開発したツール系Webアプリ

✔人に見せても恥ずかしくないコードを書こう

「リーダブルコード」は、わかりやすく良いコードの定義を教えてくれる本です。

  • 見るからにきれいなコードの書き方
  • コードの分割方法
  • 変数や関数の命名規則

エンジニアのスタンダートとすべき基準を一から解説しています。

何回も読むのに値する本なので、ぜひ手にとって読んでみてください。

アプリの紹介(ショートムービー)

一番簡単な足し算のみで試してますが、色々やってみてください!

お子さんとやると面白いかもしれません!

コード

import random, time

question_total = 0
right_answer = 0
wrong_question = []
game_continue = True

def get_type():
	right_input = {'0', '1'}
	correct_input = False
	while not correct_input:
		type_str = input("足し算の場合は「0」, 引き算の場合は「1」を入力してください→")
		if type_str in right_input:
			correct_input = True
	return type_str

def get_level():
	number_list = [(1,10), (5, 15), (11, 30), (10, 50), (51, 100)]

	right_input = {'1', '2', '3', '4', '5'}
	correct_input = False
	while not correct_input:
		level = input('レベルは1から5のどれかになります→ ')
		if level in right_input:
			correct_input = True
	list_offset = int(level) - 1	
	return number_list[list_offset]

def mondaiBool(mondaisu_str):
	tf = False
	try:
		mondaisu = int(mondaisu_str)
		if mondaisu <= 30:
			tf = True
			message=None
		else:
			message = "確実にできるところからやりましょう(問題は1度に30問まで)"
	except:
		message = '数値の入力をお願いします'
	return tf, message

def get_mondaisu():
	mondai_bool = False
	question_sentence = '何問やりますか?'
	while not mondai_bool:
		mondaisu = input(f'{question_sentence}→ ')
		mondai_bool, question_sentence = mondaiBool(mondaisu)
	return int(mondaisu)

def add(min_digit, max_digit):
	x = random.randint(min_digit, max_digit)
	y = random.randint(min_digit, max_digit)
	correct_answer = x + y
	return f'{x} + {y} = ', correct_answer

def subtract(min_digit, max_digit):
	x = random.randint(min_digit, max_digit)
	y = random.randint(min_digit, max_digit)
	if y > x: 
		x, y = y, x
	correct_answer = x - y

	while correct_answer <= x/2:
		x = random.randint(min_digit, max_digit)
		y = random.randint(min_digit, max_digit)
		if y > x: 
			x, y = y, x
		correct_answer = x - y

	return f'{x} - {y} = ', correct_answer

def continue_or_done():
	right_input = {'y', 'n', 'Y', 'N'} 
	correct_input = False 
	while not correct_input:
		yours = input("もう一度??(はい → y、いいえ → n) ⇒")
		if yours in right_input:
			correct_input = True
	yours = yours.lower()#大文字の場合、小文字にする
	if yours == 'y':
		return True
	else:
		return False

def decide_calc_type(type_str):
	if type_str == '0':
		return add
	else:
		return subtract

if __name__=="__main__":
	type_str = get_type()
	min_digit, max_digit = get_level()
	while game_continue:
		mondaisu = get_mondaisu() #return int
		question_total += mondaisu

		for _ in range(mondaisu):
			right_function = decide_calc_type(type_str) #this is the function decided by type_str
			equision, answer = right_function(min_digit, max_digit) #answer is int
			your_answer = input(equision)
			try:
				your_answer = int(your_answer)
			except:
				wrong_question.append(equision)
			else:	
				if answer == your_answer:
					right_answer += 1
				else:
					wrong_question.append(equision)

		game_continue = continue_or_done()

	print("問題終了しました。結果集計中です。")
	time.sleep(3)
	print(f"結果は、{question_total}問中、{right_answer}問正解です!")
	if right_answer != question_total:
		print("間違えてしまった問題は下記の通りです")
		for e in wrong_question:
			print(e)

「コード」をコピペして、実行してください

まとめ

programming

以上となります

前回の「じゃんけん」よりは関数ごとに少しすっきりとできたかなと思っています

解説は明日以降まとめていきますが、

前回のようにコードを一つ一つ解説ということだけでなく、

作っている過程でエラーや

その修正、改善についてや

ここの部分は作り替えるともっと精度高くなるかも!

というアプリ開発で思ったことなども記載していこうと思っています

是非立ち寄ってみてください!

また何かご意見等ございましたら、Twitterで是非絡んできてください!

それではまた明日もお願いします!

タイトルとURLをコピーしました