pythonに慣れよう4

pythonのif文を麻雀の役の計算で書いてみる。

chair = "parent"
yaku = int(input("yaku ? "))

if yaku == 1:
	print("lon, 2000")
elif yaku == 2:
	print("lon, 3900")
elif yaku == 3:
	print("lon, 7700")
elif yaku < 6:
	print("mangan, 12000")
elif yaku < 8:
	print("haneman, 18000")
elif yaku < 11:
	print("baiman, 24000")
elif yaku < 13:
	print("sanbaiman, 36000")
else:
	print("48000, shuryodesu")
&#91;/python&#93;

&#91;vagrant@localhost python&#93;$ python app.py
yaku ? 6
haneman, 18000
yaku ? 24
48000, shuryodesu

ifを入れ子にする場合。インデントでずらす。
&#91;python&#93;
chair = "child"
yaku = int(input("yaku ? "))

if chair == "parent":
	if yaku == 1:
		print("lon, 2000")
	elif yaku == 2:
		print("lon, 3900")
	elif yaku == 3:
		print("lon, 7700")
	elif yaku < 6:
		print("mangan, 12000")
	elif yaku < 8:
		print("haneman, 18000")
	elif yaku < 11:
		print("baiman, 24000")
	elif yaku < 13:
		print("sanbaiman, 36000")
	else:
		print("48000, shuryodesu")
elif chair == "child":
	if yaku == 1:
		print("lon, 1500")
	elif yaku == 2:
		print("lon, 2900")
	elif yaku == 3:
		print("lon, 5800")
	elif yaku < 6:
		print("mangan, 8000")
	elif yaku < 8:
		print("haneman, 12000")
	elif yaku < 11:
		print("baiman, 18000")
	elif yaku < 13:
		print("sanbaiman, 24000")
	else:
		print("36000, shuryodesu")
else:
	print("see you again")
&#91;/python&#93;

&#91;vagrant@localhost python&#93;$ python app.py
yaku ? 4
mangan, 8000

Yes sir!

改行せずに1行で書くこともできる。
&#91;python&#93;
chair = "child"
yaku = int(input("yaku ? "))

print("tobimashita" if yaku > 4 else "I'm winner")

while文

i = 1
while i < 29: print("2月{0}日".format(i)) i += 1 [/python]