Practice applying functions and global variables in Python

Introduction

Thank you for browsing. Please forgive English grammar. Please kindly tell the experts if there is something you can do about it.

Overview

・ Vote for A or B. ・ Check the number of votes and initialize the number of votes. ・ Perform termination processing.

Completion example

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.]")

Execution example

 [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.]

Creating a function to vote for A and B

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

Creating a function to check the number of votes

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,".]")

Creating a function to initialize the number of votes

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

Creating a sentence to vote for each AB

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.]:"))

Create a sentence to confirm the number of votes

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.]:"))

Create a statement to initialize the number of votes

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.]:"))

Create a statement to choose whether to vote A, B, check the number of votes, initialize the number of votes, or end

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.]:"))

Complete

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.]")

in conclusion

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

Practice applying functions and global variables in Python
Using global variables in python functions
[Python3] Dynamically define global variables in functions
Difference between nonlocal and global in Python
Global and local variables 2
Python variables and data types learned in chemoinformatics
[python] Difference between variables and self. Variables in class
Global and local variables 1
Difference between Ruby and Python in terms of variables
A note on handling variables in Python recursive functions
Python: Class and instance variables
About Python variables and objects
Handle environment variables in Python
Overriding library functions in Python
Python class variables and instance variables
Stack and Queue in Python
Introducing Python in Practice (PiP)
Python variables and object IDs
Unittest and CI in Python
Python 3 sorted and comparison functions
Python functions learned in chemoinformatics
Python higher-order functions and comprehensions
Applied practice of try/except and dictionary editing and retrieval in Python
Reference order of class variables and instance variables in "self. Class variables" in Python
Display numbers and letters assigned to variables in python print
Comparison of how to use higher-order functions in Python 2 and 3
Initializing global variables using Python decorators
Difference between list () and [] in Python
Difference between == and is in python
Landmines hidden in Python class variables
Manipulate files and folders in Python
About python dict and sorted functions
Assignments and changes in Python objects
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Export and output files in Python
Dynamically define functions (methods) in Python
Algorithm (segment tree) in Python (practice)
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
How to use functions in separate files Perl and Python versions
Precautions when passing def to sorted and groupby functions in Python? ??
Python3> Functions> Symbol table> Assign to variables / Reference variables / Global variables / globals () / locals ()
I was addicted to confusing class variables and instance variables in Python
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Differences between Ruby and Python in scope
How to access environment variables in Python
[Python] Use and and or when creating variables
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Implementation module "deque" in queue and Python
Easily use your own functions in Python