Python 7 Levels, L5, Unit 45, Use of Functions

Powered By EmbedPress

Exercise 1:

def list_slice(list1):
    list2 = []#create an empty list
    for i in list1:
        if i%2 != 0:
            list2.append(i)
    return list2

l = [3,9,6,18,28,33,45,80,103]
l2 = list_slice(l)
for i in l2:
    print(i)

Exercise 2:

import turtle as t
t.penup()
t.goto(-200,0)
t.pendown()
t.speed(0)
t.color("red")
def star(size):
    if size < 10:
        return
    else:
        for i in range(5):
            t.forward(size)
            t.left(144)
            star(size/3)

star(360)

Exercise 3:

def list_max(list1):
    max1 = list1[0]
    for i in list1:
        if i > max1:
            max1 = i
    return max1

list1 = [4,56,2,47,11]
result = list_max(list1)
print(result)

Exercise 4:

def seek(num,list1):
    for i in list1:
        if i == num:
            return True
    else:
        return False

list1 = [1,23,4,5,100,109]
print(seek(6,list1))

Exercise 5:

def maopao(list1):
    for i in range(len(list1)-1):
        for j in range(len(list1)-1-i):
            if list1[j] < list1[j+1]:
                t = list1[j]
                list1[j] = list1[j+1]
                list1[j+1] = t
    return list1

list1 = [4,52,56,1,3,6,100,89]
for i in maopao(list1):
    print(i)

Calendar:

year_in = int(input("Please input year:"))
month_in = int(input("Please input month:"))
sum_day = 0
month_sumDay = 0

def leap_year(year):
    if(year%4 == 0 and year%100 != 0 ) or (year%400 == 0):
        return True
    else:
        return False

for year in range(1900,year_in):#find days from Jan. 1 1900 to Dec. 31 (year_in-1)
    if leap_year(year):
        sum_day += 366
    else:
        sum_day +=365
for month in range(1,month_in):#find days from Jan. 1 to (month-1). 30
    if month == 2:
        if leap_year(year_in):
            sum_day +=29
        else:
            sum_day+=28
    elif month == 4 or month == 6 or month ==9 or month == 11:
        sum_day += 30
    else:
        sum_day += 31
sum_day+=1#Add current month day
week = sum_day%7
if month_in == 2:
    if leap_year(year_in):
        month_sumDay = 29
    else:
        month_sumDay = 28
elif month_in == 4 or month_in == 6 or month_in == 9 or month_in == 11 :
    month_sumDay = 30
else:
    month_sumDay = 31
print("\t\t",year_in,"Year",month_in,"Month")
print("Sun.\tMon.\tTue.\tWed.\tThu.\tFri.\tSat.")
for i in range(0,week):
    print(end = "\t\t")
for day in range(1,month_sumDay+1):
    print(day,end = "\t\t")
    if sum_day%7 == 6:
        print()
    sum_day +=1

Homework:

student_all = []#2 dimentional list, each item in the list is a student information

def seek(name,list1):
    for i in range(len(list1)):
        if name in list1[i]:
            return i
    else:
        return -1

def bubble(score1,score2):
    for i in range(len(score2)-1):
        for j in range(len(score2)-1-i):
            if score1[j] < score1[j+1]:
                s = score1[j]
                score1[j] = score1[j+1]
                score1[j+1] = s

                student1 = score2[j]
                score2[j] = score2[j+1]
                score2[j+1] = student1
                         
while True:
    number = input("Please pick an operation:\n"\
                   "1.Add Student\n2.Search Student\n3.Delete Student\n4.Update Student\n5.View All Student\n"
                   "6.Input Scores\n7.Total Score Ranking\n8.Ranking of One Course\n99.Quit\n")
    if number == "99":
        print("Abort system.")
        break
    elif number == "1":
        student = []#the list for one student, containing all the information of a student
        name = input("Please input student name:")
        age = int(input("Please input student age:"))
        french = 0
        math = 0
        english = 0
        sum_score = 0
        avg_score = 0
        student.append(name)
        student.append(age)
        student.append(french)
        student.append(math)
        student.append(english)
        student.append(sum_score)
        student.append(avg_score)
        student_all.append(student)#add this student to the student list

        print("Name\tAge\tFrench\tMath\tEnglish\tTotal Score\tAverage Score")
        for i in student:
            print(i,end = "   \t" )
        print()#output a new line

    elif number == "2":
        name = input("Please input student name you look for:")
        result = seek(name,student_all)
        if  result == -1:
            print(name," does not exist.")
        else:
            print("Name\tAge\tFrench\tMath\tEnglish\tTotal Score\tAverage Score")
            for j in student_all[result]:
                print(j,end = "   \t")
            print()#Output a new line
        
            
    elif number == "3":
        name = input("Please input the student name to be deleted:")
        result = seek(name,student_all)
        if  result == -1:
            print(name," does not exist.")
        else:
            del student_all[result]
            print(name," has been deleted.")

    elif number == "4":
        name = input("Please input the student name to be updated:")
        result = seek(name,student_all)
        if  result == -1:
            print(name," does not exist.")
        else:
            name = input("Please input updated name:")
            age = int(input("Please input updated age:"))
            student_all[result][0] = name
            student_all[result][1] = age
            print("Update succeeds.")
            print("Name\tAge\tFrench\tMath\tEnglish\tTotal Score\tAverage Score")
            for j in student_all[result]:
                print(j,end = "   \t")
            print()#Output a new line
       

    elif number == "5":
        print("Name\tAge\tFrench\tMath\tEnglish\tTotal Score\tAverage Score")
        for i in range(len(student_all)):
            for j in range(len(student_all[i])):
                print(student_all[i][j],end = "   \t")
            print()#Output a new line

    elif number == "6":
        name = input("Please input the student name whose score you will input:")
        result = seek(name,student_all)
        if  result == -1:
            print(name," does not exist.")
        else:
            french = int(input("Please input " + name + "'s French score:"))
            math = int(input("Please input " + name + "'s Math score:"))
            english = int(input("Please input " + name + "'s English score:"))
            sum_score = french + math + english
            avg_score = sum_score//3
            '''Store the score in the student list'''
            student_all[result][2] = french
            student_all[result][3] = math
            student_all[result][4] = english
            student_all[result][5] = sum_score
            student_all[result][6] = avg_score

            print("Name\tAge\tFrench\tMath\tEnglish\tTotal Score\tAverage Score")
            for i in student_all[result]:
                print(i,end = "   \t")
            print()#Output a new line
                
                
    elif number == "7":
        score = []#used for storing student's total score
        for i in range(len(student_all)):
            score.append(student_all[i][5])

        all = student_all[:]
        '''bubble sort duplicated list based on total scores,'''
        bubble(score, all)
        print("Name\tAge\tFrench\tMath\tEnglish\tTotal Score\tAverage Score\tRanking")
        for i in range(len(all)):
            for j in range(len(all[i])):
                print(all[i][j],end = "   \t")
            print(i+1)#Output a new line
    elif number == "8":
        score = []#used for storing score of one course
        course_name = int(input("Please pick one course(1.French 2.Math 3.English)"))
        if course_name == 1 or course_name == 2 or course_name == 3:
            for i in range(len(student_all)):
                score.append(student_all[i][course_name+1])
        all = student_all[:]
        '''bubble sort the duplicated list based on the score'''
        bubble(score, all)
        print("Name\tAge\tFrench\tMath\tEnglish\tTotal Score\tAverage Score\tRanking")
        for i in range(len(all)):
            for j in range(len(all[i])):
                print(all[i][j],end = "   \t")
            print(i+1)#Output a new line

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts