def getPentagonaNumber(n):
m = n * (3 * n - 1) / 2
print(format(m, "8.0f"), end = '')
k = 100
for i in range(1, k + 1):
getPentagonaNumber(i)
if i % 10 == 0:
print()
sec6.2
def sumDigits(n):
x = 0
while n > 10:
x = x + n % 10
n = n // 10
x = x + n
print(x)
y = eval(input("number: "))
sumDigits(y)
sec6.5
def displaySortedNumbers(num1, num2, num3):
if num1 > num2:
num1, num2 = num2, num1
if num1 > num3:
num1, num3 = num3, num1
if num2 > num3:
num2, num3 = num3, num2
print("The sorted numbers are ", num1, num2, num3)
x, y, z = eval(input("Enter three numbers: "))
displaySortedNumbers(x, y, z)
sec6.8
def celsiusToFahrenheit(celsius):
fahreheit = (9 / 5) * celsius + 32
return (format(fahreheit, "10.2f"))
def fahrenheitToCelsius(fahreheit):
celsius = (5 / 9) * (fahreheit - 32)
return (format(celsius, "8.2f"))
print("CelsiustFahrenheitt|tFahrenheittCelsius")
for i in range(10):
m = 40 - i
n = 120 - i * 10
print(m, 't', celsiusToFahrenheit(m), 't|t',
n, 't', fahrenheitToCelsius(n))
sec6.10
# Check whether number is prime
def isPrime(number):
divisor = 2
while divisor <= number / 2:
if number % divisor == 0:
# If true, number is not prime
return False # number is not a prime
divisor += 1
return True # number is prime
count = 0
for i in range(1, 10000 + 1):
if isPrime(i):
print(format(i, "<6.0f"), end='')
count += 1
if count % 20 == 0:
print()
sec6.16
def numberOfDaysInAYear(year):
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
year = 366
else:
year =365
return year
for i in range(2010, 2020 + 1):
print(i, "year:", numberOfDaysInAYear(i))
sec6.37
from random import randint
import turtle
def getRandomCharacter(ch1, ch2):
return chr(randint(ord(ch1), ord(ch2)))
def getRandomLowerCaseLetter():
return getRandomCharacter('a', 'z')
for i in range(1, 101):
s = getRandomLowerCaseLetter()
turtle.write(s, font=(8))
turtle.penup()
turtle.forward(20)
if i % 15 == 0:
turtle.goto(0, -i*2)
turtle.pendown()
turtle.done()
sec6.38
import turtle
def drawLine(x1, y1, x2, y2, color = "black", size = 1):
turtle.color(color)
turtle.pensize(size)
turtle.penup()
turtle.goto(x1, y1)
turtle.pendown()
turtle.goto(x2, y2)
turtle.hideturtle()
turtle.done()
drawLine(3,4,100,200,"red",7)
sec6.47
import turtle
def drawChessboard(startx, endx, starty, endy):
turtle.speed(50000)
i = abs((endx - startx) / 8)
j = abs((endy - starty) / 8)
count = 1
x = startx
y = starty
while x < endx:
while y > endy:
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
if count % 2 == 0:
turtle.begin_fill()
turtle.color("black")
turtle.forward(i)
turtle.right(90)
turtle.forward(j)
turtle.right(90)
turtle.forward(i)
turtle.right(90)
turtle.forward(j)
turtle.right(90)
turtle.end_fill() # draw black
else:
turtle.color("black")
turtle.forward(i)
turtle.right(90)
turtle.forward(j)
turtle.right(90)
turtle.forward(i)
turtle.right(90)
turtle.forward(j)
turtle.right(90) # draw white
count += 1
y -= j
count += 1
y = starty
x += i
drawChessboard(-300, -100, 100, -100)
drawChessboard(100, 200, 100, 0)
turtle.hideturtle()
turtle.done()
ckp6.1
''' Function can be used to define reusable code and organize and simplify code. '''ckp6.2
''' A function definition consists of the function's name, parameters, and body. Calling a function executes the code in the function. There are two ways to call a function, depending on whether or not it returns a value. If the function returns a value, a call to that function is usually treated as a value. If a function does not return a value, the call to the function must be a statement. '''ckp6.3
# Return the max of two numbers
def max(num1, num2):
result = num1 if num1 > num2 else num2
return result
def main():
i = 5
j = 2
k = max(i, j) # Call the max function
print("The larger number of", i, "and", j, "is", k)
main() # Call the main function
ckp6.4
True
ckp6.5def xFunction(x, y):
print(x + y)
return
xFunction(1, 2)
'''
Yes!
'''
ckp6.6
''' A function contains a header and body. The header begins with the def keyword, followed by the function’s name and parameters, and ends with a colon. The variables in the function header are known as formal parameters or simply parameters. A parameter is like a placeholder: When a function is invoked, you pass a value to the parameter. This value is referred to as an actual parameter or argument. Parameters are optional; that is, a function may not have any parameters. For example, the random.random() function has no parameters. '''ckp6.7
''' def salesCommission(amount, rate): value def printCalender(month, year): not value def squareRoot(number): value def isEven(number): not value def printMessage(message, times): not value def monthlyPayment(loan, years, interest): value def lowercaseLetter(letter): not value '''ckp6.8
def function1(n, m):
function2(3.4)
def function2(n):
if n > 0:
return 1
elif n == 0:
return 0
elif n < 0:
return -1
function1(2, 3)
ckp6.9
def main():
print(min(5, 6))
def min(n1, n2):
smallest = n1
if n2 < smallest:
smallest = n2
main() # Call the main function
ckp6.10
def main():
print(min(min(5, 6), (51, 6)))
def min(n1, n2):
smallest = n1
if n2 < smallest:
smallest = n2
main() # Call the main function
ckp6.12
''' Suppose a function header is as follows: def f(p1, p2, p3, p4): Which of the following calls are correct? f(1, p2 = 3, p3 = 4, p4 = 4) true f(1, p2 = 3, 4, p4 = 4) false f(p1 = 1, p2 = 3, 4, p4 = 4) false f(p1 = 1, p2 = 3, p3 = 4, p4 = 4) true f(p4 = 1, p2 = 3, p3 = 4, p1 = 4) true '''ckp6.15
'''
def main():
max = 0
getMax(1, 2, max)
print(max)
def getMax(value1, value2, max):
if value1 > value2:
max = value1
else:
max = value2
main()
def main():
i = 1
while i <= 6:
print(function1(i, 2))
i += 1
def function1(i, num):
line = ""
for j in range(1, i):
line += str(num) + " "
num *= 2
return line
main()
def main():
# Initialize times
times = 3
print("Before the call, variable", "times is", times)
# Invoke nPrintln and display times
nPrint("Welcome to CS!", times)
print("After the call, variable", "times is", times)
# Print the message n times
def nPrint(message, n):
while n > 0:
print("n = ", n)
print(message)
n -= 1
main()
'''
def main():
i = 0
while i <= 4:
function1(i)
i += 1
print("i is", i)
def function1(i):
line = " "
while i >= 1:
if i % 3 != 0:
line += str(i) + " "
i -= 1
print(line)
main()
ckp6.16
def main():
max = 0
getMax(1, 2, max)
def getMax(value1, value2, max):
if value1 > value2:
max = value1
else:
max = value2
print(max)
main()
ckp6.17
'''
def function(x):
print(x)
x = 4.5
y = 3.4
print(y)
x = 2
y = 4
function(x)
print(x)
print(y)
'''
def f(x, y = 1, z = 2):
return x + y + z
print(f(1, 1, 1))
print(f(y = 1, x = 2, z = 3))
print(f(1, z = 3))
ckp6.18
def function():
x = 4.5
y = 3.4
print(x)
print(y)
function()
print(x)
print(y)
'''
x, y都是不是实参,且是函数里的东西。没有确定的值
'''
ckp6.19
x = 10
if x < 0:
y = -1
else:
y = 1
print("y is", y)
ckp6.20
def f(w = 1, h = 2):
print(w, h)
f()
f(w = 5)
f(h = 24)
f(4, 5)
ckp6.21
def main():
nPrintln(5)
def nPrintln(n, message = "Welcome to Python!"):
for i in range(n):
print(message)
main() # Call the main function
ckp6.22
''' With default arguments, you can define a function once, and call the function in many different ways. This achieves the same effect as defining multiple functions with the same name in other programming languages. If you define multiple functions in Python,the later definition replaces the previous definitions. '''ckp6.23
def f(x, y):
return x + y, x - y, x * y, x / y
t1, t2, t3, t4 = f(9, 5)
print(t1, t2, t3, t4)
#一个函数可以输出多个数值



