- 阅读权限
- 255
- 威望
- 0 级
- 论坛币
- 49957 个
- 通用积分
- 79.5487
- 学术水平
- 253 点
- 热心指数
- 300 点
- 信用等级
- 208 点
- 经验
- 41518 点
- 帖子
- 3256
- 精华
- 14
- 在线时间
- 766 小时
- 注册时间
- 2006-5-4
- 最后登录
- 2022-11-6
|
- def temperature(t):
- 'prints message based on temperature value t'
- if t > 86:
- print('It is hot!')
- elif t > 32:
- print('It is cool.')
- else: # t <= 32
- print('It is freezing!')
- def sorted(lst):
- 'returns True if sequence lst is increasing, False otherwise'
- for i in range(0, len(lst)-1): # i = 0, 1, 2, ..., len(lst)-2
- if lst[i] > lst[i+1]:
- return False
- return True
- def nested(n):
- 'prints n lines each containing values 0 1 2 ... n-1'
- for j in range(n): # repeat n times:
- for i in range(n): # print 0, 1, ..., n-1
- print(i, end=" ")
- print() # move cursor to next line
- def nested2(n):
- 'prints n lines 0 1 2 ... j for j = 0, 1, ..., n-1'
- for j in range(n): # j = 0, 1, ..., n-1
- for i in range(j+1): # print 0 1 2 ... j
- print(i, end=" ")
- print() # move to next line
- def print2D(t):
- 'prints values in 2D list t as a 2D table'
- for row in t:
- for item in row: # print item followed by
- print(item, end=' ') # a blank space
- print() # move to next line
- def incr2D(t):
- 'increments each number in 2D list of numbers t'
- nrows = len(t) # number of rows
- ncols = len(t[0]) # number of columns
- for i in range(nrows): # i is the row index
- for j in range(ncols): # j is the column index
- t[i][j] += 1
- def fibonacci(bound):
- 'returns the smallest Fibonacci number greater than bound'
- previous = 1 # first Fibonacci number
- current = 1 # second Fibonacci number
- while current <= bound:
- # current becomes previous, and new current is computed
- previous, current = current, previous+current
- return current
- def hello2():
- '''a greeting service; it repeatedly requests the name
- of the user and then greets the user'''
- while True:
- name = input('What is your name? ')
- print('Hello {}'.format(name))
- def cities():
- '''returns the list of cities that are interactively entered
- by the user; the empty string ends the interactive input'''
- lst = []
-
- city = input('Enter city: ') # ask user to enter first city
- while city != '': # if city is not the flag value
- lst.append(city) # append city to list
- city = input('Enter city: ') # and ask user once again
- return lst
- def cities2():
- '''returns the list of cities that are interactively entered
- by the user; the empty string ends the interactive input'''
- lst = []
- while True: # forever repeat:
- city = input('Enter city: ') # ask user to enter city
- if city == '': # if city is the flag value
- return lst # return list
- lst.append(city) # append city to list
- def print2D2(table):
- 'prints values in 2D list of numbers t as a 2D table'
- for row in table:
- for entry in row:
- print(entry, end=' ')
- print()
- def before0(table):
- '''prints values in 2D list of numbers t as a 2D table;
- only values in row up to first 0 are printed'''
- for row in table:
- for num in row: # inner for loop
- if num == 0: # if num is 0
- break # terminate inner for loop
- print(num, end=' ') # otherwise print num
- print() # move cursor to next line
- def ignore0(table):
- '''prints values in 2D list of numbers t as a 2D table;
- 0 values are no printed'''
- for row in table:
- for num in row: # inner for loop
- if num == 0: # if num is 0, terminate
- continue # current inner loop iteration
- print(num, end=' ') # otherwise print num
- print() # move cursor to next line
复制代码
|
|