1. 程式人生 > >python考試程式設計題

python考試程式設計題

3.

a: while True:     s = raw_input('變數名為:')     if s == 'exit':         print '退出'         break     #判斷是否由字母或下劃線組成     if s[0].isalpha() or s[0] == '_':         for i in s[1:]:             if not (i.isalnum() or i == '_'):                 print '%s變數名不合法 ' % s                 break         else:             print '%s 變數名合法' % s     else:         print '%s變數名不合法' % s

b: count = 0 for i in range(1, 100):     if i % 2 == 0:         count -= i     else:         count += i print count

c: num = range(1, 13) i = 1 while i < 13:     if (i == 6 or i == 10):         print ' ',     else:         print i,     i += 1

h:

li = ['frdgrfgdsHHJJ', 'cdfregHHHJDGF'] new_li = [] for i in li:     new_li.append(i.lower())

print(new_li)

7.

li=['happy','lucky','linux'] print len(li) li.append('seven') print li li.insert(0,'Tony') print li li[1]='Kelly' print li print li.pop(1) print li li.pop(2) print li print li[::-1] print li

8.

dic = {'k1': 'v1', 'k2': 'v2', 'k3': [11, 22, 33]}

# 1.

for i in dic:      print(i)

# 2.  for v in dic.values():      print(v)

 # 3.  for k in dic:     print(k, dic[k])  # 4.  dic['k1'] = 'harry'  print(dic)

 # 5.  dic['k3'].append('44')  print(dic)

# 6. dic['k3'].insert(0, 18) print(dic)

9.

s = raw_input('請輸入英文句子:') s_new = s.split()

dict = {}

for item in s_new:     if item not in dict:         dict[item] = 1     else:         dict[item] += 1 print dict

10.

def fun(set):     bigger = []     smaller = []     for i in set:         if i < 66:             smaller.append(i)         else:             bigger.append(i)     dic = {'k2': smaller, 'k1': bigger, 'k2': smaller}     print(dic)

list = [11, 22, 33, 44, 55, 66, 77, 88, 99, 90] fun(list)

12.

i=1 while i<=9:     j=1     while j<=i:         print '%d*%d=%d\t'%(i,j,i*j),         j+=1     print ''     i+=1

13.

num1 = int(raw_input('請輸入第一個數:')) num2 = int(raw_input('請輸入第二個數:')) min_num = min(num1, num2) for i in range(1, min_num + 1):     if num1 % i == 0 and num2 % i == 0:         biger_count = i smaller_count = (num1 * num2) / biger_count

print '最大公約數為:%d' % biger_count print '最小公倍數為:%d' % smaller_count

21.

class People(object):     __name = 'luffy'     __age = 18

pl = People() print(pl.__name, pl.__name)

# 出現報錯,因為私有屬性不允許直接訪問

22.

class Parent(object):     x = 1

class Child1(Parent):     pass

class Child2(Parent):     pass

print(Parent.x, Child1.x, Child2.x) Child1.x = 2 print(Parent.x, Child1.x, Child2.x) Parent.x = 3 print(Parent.x, Child1.x, Child2.x)

23.

class Person(object):     def __init__(self, name):         self.name = name

    def buy_car(self, car):         print '%s 寶馬BMW 4s店買%s' % (self.name, car)

joker = Person('joker') joker.buy_car('BMW X7')

class B:     def handle(self):         print '喵喵'

class A(B):     def handle(self):         B.handle(self)

new = A() new.handle()

25.

class Student(object):     count = 0

    def __init__(self, name, age):         self.name = name         self.age = age         Student.count += 1

    @staticmethod     def count_student():         print '共例項%d個物件' % Student.count

bob = Student('bob', 19) Jenny = Student('Jenny', 18) Danny = Student('Danny', 19) liming = Student('liming', 20)

Student.count_student()

26.

class Student(object):     count = 0

    def __init__(self, name, age):         self.name = name         self.age = age         Student.count += 1

    @staticmethod     def count_student():         print '共例項%d個物件' % Student.count

bob = Student('bob', 19) Jenny = Student('Jenny', 18) Danny = Student('Danny', 19) liming = Student('liming', 20)

Student.count_student()