#はじめに
閲覧していただきありがとうございます。
英語の文法は許してください。
有識者の方へ、もっとなんとかなるとかあったら優しく教えてください。
#概要
・AかBに投票をする。
・投票数の確認と投票数の初期化をする。
・終了処理をする。
#完成例
def voteA ():
global vote1
vote1 = vote1 + 1
def voteB ():
global vote2
vote2 = vote2 + 1
def count ():
global vote1
global vote2
print("[The number of votes for A is",vote1,".]")
print("[The number of votes for B is",vote2,".]")
def Initialize ():
global vote1
global vote2
vote1 = 0
vote2 = 0
op = 0
while op != 9:
if op == 1:
voteA ()
print("[Vote for A.]")
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
elif op == 2:
voteB ()
print("[Vote for B.]")
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
elif op == 3:
print("[Check the vote count.]")
count ()
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
elif op == 0:
print("[Initialize the vote count.]")
Initialize ()
count ()
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
else:
while op != 1 and op != 2 and op != 3 and op != 9 and op != 0:
print("[Vote for A.>1]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
print("[Good bye.]")
#実行例
[Initialize the vote count.]
[The number of votes for A is 0 .]
[The number of votes for B is 0 .]
[Vote for A.>1]
[Vote for B.>2]
[Check the vote count.>3]
[End.>9]
[Initialize the vote count.>0]
'[Chose 1 or 2 or 3 or 9 or 0.]:1
[Vote for A.]
[Vote for A.>1]
[Vote for B.>2]
[Check the vote count.>3]
[End.>9]
[Initialize the vote count.>0]
'[Chose 1 or 2 or 3 or 9 or 0.]:1
[Vote for A.]
[Vote for A.>1]
[Vote for B.>2]
[Check the vote count.>3]
[End.>9]
[Initialize the vote count.>0]
'[Chose 1 or 2 or 3 or 9 or 0.]:2
[Vote for B.]
[Vote for A.>1]
[Vote for B.>2]
[Check the vote count.>3]
[End.>9]
[Initialize the vote count.>0]
'[Chose 1 or 2 or 3 or 9 or 0.]:3
[Check the vote count.]
[The number of votes for A is 2 .]
[The number of votes for B is 1 .]
[Vote for A.>1]
[Vote for B.>2]
[Check the vote count.>3]
[End.>9]
[Initialize the vote count.>0]
'[Chose 1 or 2 or 3 or 9 or 0.]:0
[Initialize the vote count.]
[The number of votes for A is 0 .]
[The number of votes for B is 0 .]
[Vote for A.>1]
[Vote for B.>2]
[Check the vote count.>3]
[End.>9]
[Initialize the vote count.>0]
'[Chose 1 or 2 or 3 or 9 or 0.]:9
[Good bye.]
#AとBに投票する関数の作成
今回は複数の関数で同じ変数を使用するため、グローバル変数を使います。
def voteA ():
global vote1
vote1 = vote1 + 1
def voteB ():
global vote2
vote2 = vote2 + 1
#投票数を確認する関数の作成
シンプルにグローバル変数であるvote1とvote2を表示させる。
def count ():
global vote1
global vote2
print("[The number of votes for A is",vote1,".]")
print("[The number of votes for B is",vote2,".]")
#投票数を初期化する関数の作成
グローバル変数のため複数の関数で同じ変数を使える。
こちらもシンプルにvote1とvote2を0にしている。
def Initialize ():
global vote1
global vote2
vote1 = 0
vote2 = 0
#ABそれぞれに投票する文の作成
Bに投票する文はAとほとんど変わらないので割愛。
opは後にwhileでループするときに使う。
voteA ()
print("[Vote for A.]")
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
#投票数を確認する文を作成
シンプルに関数を呼び出すだけ。
print("[Check the vote count.]")
count ()
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
#投票数を初期化する文を作成
こちらも、シンプルに関数を呼び出すだけ。
正しく初期化されているかを確認するために、投票数を確認する関数も呼び出しておく。
print("[Initialize the vote count.]")
Initialize ()
count ()
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
#A,Bを投票するか、投票数を確認するか、投票数を初期化するか、終了するかを選択する文を作成
whileにそれぞれ挿入していく。
最初にopを0に宣言しておくことで、投票の初期化と確認をしてから開始できる。
できるだけ強制終了であるbrakeを使用したくないため、終了の9を入力したらwhileのループが終了するようになっている。
最後のifでは誤った選択肢を選択された場合に正しい選択肢が選択されるまでwhileでループさせている。
op = 0
while op != 9:
if op == 1:
voteA ()
print("[Vote for A.]")
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
elif op == 2:
voteB ()
print("[Vote for B.]")
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
elif op == 3:
print("[Check the vote count.]")
count ()
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
elif op == 0:
print("[Initialize the vote count.]")
Initialize ()
count ()
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
else:
while op != 1 and op != 2 and op != 3 and op != 9 and op != 0:
print("[Vote for A.>1]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
#完成
全部組み合わせて完成。
def voteA ():
global vote1
vote1 = vote1 + 1
def voteB ():
global vote2
vote2 = vote2 + 1
def count ():
global vote1
global vote2
print("[The number of votes for A is",vote1,".]")
print("[The number of votes for B is",vote2,".]")
def Initialize ():
global vote1
global vote2
vote1 = 0
vote2 = 0
op = 0
while op != 9:
if op == 1:
voteA ()
print("[Vote for A.]")
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
elif op == 2:
voteB ()
print("[Vote for B.]")
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
elif op == 3:
print("[Check the vote count.]")
count ()
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
elif op == 0:
print("[Initialize the vote count.]")
Initialize ()
count ()
print("[Vote for A.>1]","\n","[Vote for B.>2]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
else:
while op != 1 and op != 2 and op != 3 and op != 9 and op != 0:
print("[Vote for A.>1]","\n","[Check the vote count.>3]","\n","[End.>9]","\n","[Initialize the vote count.>0]")
op = int(input("[Chose 1 or 2 or 3 or 9 or 0.]:"))
print("[Good bye.]")
#おわりに
グローバル変数はとっても便利でおもしろい。
終了するときGood Bye.てでるのすごいかっこいい。
もっと関数の勉強をしたい。