Thank you for browsing. Please forgive English grammar. Please kindly tell the experts if there is something you can do about it.
・ Vote for A or B. ・ Check the number of votes and initialize the number of votes. ・ Perform termination processing.
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.]
This time we will use global variables because we will use the same variables in multiple functions.
def voteA ():
global vote1
vote1 = vote1 + 1
def voteB ():
global vote2
vote2 = vote2 + 1
Simply display the global variables vote1 and 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,".]")
Since it is a global variable, the same variable can be used in multiple functions. This also simply sets vote1 and vote2 to 0.
def Initialize ():
global vote1
global vote2
vote1 = 0
vote2 = 0
The sentence to vote for B is almost the same as A, so I will omit it. op will be used later when looping with 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.]:"))
Simply call the function.
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.]:"))
Again, simply call the function. In order to check if it is initialized correctly, we also call a function that checks the number of votes.
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.]:"))
Insert each into while. By declaring op to 0 first, you can start after initializing and confirming the vote. I don't want to use brake, which is a forced termination as much as possible, so when I enter 9 for termination, the while loop ends. In the last if, if the wrong option is selected, it loops with while until the correct option is selected.
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.]:"))
Completed by combining all.
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.]")
Global variables are very convenient and interesting. Good Bye. When it's finished, it's really cool.
I want to study more functions.
Recommended Posts