Python core programming second edition - the fifth chapter exercises - write yourself (welcome correction)

second question: operator

1/ write a function, calculate and return the product of two numbers; 2/Write a piece of code to call this function and display its result

def ji(a,b):
    return a*b

third question: standard type operator. Write a script, enter a test score, and output his score (A-F) according to the following criteria.

A:90~100 B:80-89 C:70-79 D:60-69 E:<60

def fenshu(a):
    if a<60:
        return 'F'
    elif 60<=a<69:
        return 'D'
    elif 70<=a<79:
        return 'C'
    elif 80<=a<89:
        return 'B'
        return 'A'

四题 Residual. Determine if a given year is a leap year.

def runnian(a):
    If (a % 4==0 and a % 100!=0) or (a % 400==0):
        Return '闰年'
        Return 'non-leap year'

Def runnian(a):
    If (a % 4==0 and a % 100!=0) or (a % 400==0):
        Return ('%d is two years' % ​​a)
        Return ('%d is not a leap year' % a)

##Interactive programming
Year = int(input('Please enter the year:'))
If (year % 4==0 and year % 100!=0) or (year % 400==0) :
    Print ('%d is leap year ' % year)
Else :
    Print ('%d is not a leap year ' % year)

Please enter the year: 1992
1992 is the year 
  • here using input instead of raw-input, because in python3 raw-input is replaced by input. If raw-input appears, the ‘name ‘raw_input’ is not defined’ error will appear.

五题 Residual Take any amount less than $1, then calculate how many coins can be replaced. The coin has 1 cent, 5 cents, 10 cents, and 25 cents.

dollar = int(input('Please enter the number of dollars: '))
Cent = dollar*100

a = cent/25
Cent = cent%25
If cent>0:
    b = cent/10
    Cent = cent%10
If cent>0:
    c = cent/5
    Cent = cent%5
If cent>0:
    d = cent

#First determine whether the variable exists, and then output, avoiding NameError
If 'a' in locals():
    Print('\n%d25 cents') % a
If 'b' in locals():
    Print('\n%d10 cents') % b
If 'c' in locals():
    Print('\n%d5 cents') % c
If 'd' in locals():
    Print('\n%d1 cents') % d