The second wish is to write an application

The second wish is to write an application


Functions
- reusable code used to perform certain action

syntax:

def  func_name:
    action 1
     ....
    return

def multiple_args(name, *args):
    print(name)
    print(args)

def multiple_keyargs(name, **kwargs):
    print(name)
    print(kwargs["role"],kwargs["power_level"])

multiple_args("Goku","hero","infinity")
multiple_keyargs(name="Goku",role="hero",power_level="infinity", finishing_move="kameyameah")




Files

charecters = []


def get_char_title():
char_title = []
for charecter in charecters:
char_title.append(charecter["name"].title())
return char_title


def print_char_title():
char_title = get_char_title()
print(char_title)


def add_char(name, chr_id=001):
charecter = {"name": name, "chr_id": chr_id}
charecters.append(charecter)


def save_file(charecter):
try:
f = open("charecters.txt", "a")
f.write(charecter + "\n")
f.close()
except Exception:
print("Could not save file")


def read_file():
try:
f = open("charecters.txt", "r")
for charecter in f.readlines():
add_char(charecter)
f.close()
except Exception:
print("Could not read file")

# read_file()

read_file()
print_char_title()
char_name = input("Enter charecter name: ")
chr_id = input("Enter charecter ID: ")

add_char(char_name, chr_id)
print_char_title()
save_file(char_name)


Yield

charecters = []


def read_file():
try:
f = open("charecters.txt", "r")
for charecter in read_charecters(f):
charecters.append(charecter)
f.close()
except Exception:
print("Could not read file")


def read_charecters(f):
for line in f:
yield line


read_file()
print(charecters)


Lambda

def twice(x):
    return x*2

twice = lambda  x: x * 2



more learning on :
* Lambda
* Generator
* Yield

Comments

Popular posts from this blog

Java Script Learning Notes

The World Knows What's First - It's Everything in one