題目出處:vamei
1.寫乙個程式,判斷2023年是否是閏年。
寫乙個程式,用於計算2023年10月1日是這一年的第幾天?(2023年1月1日是這一年的第一天)
這些小題目是為了方便大家加深對python理解而設計的。
def isleapyear(year):
if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):
return true
else:
return false
def countday(year,month,day):
month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if isleapyear(year):
month[1]=29
day = sum(month[0:month]) - month[month-1] + day
return day
print isleapyear(2000),countday(2000,9,9)
print countday(*[2001,5,1])
輸出結果為:
true 253
1212.
有乙個record.txt的文件,內容如下:
# name, age, score
tom, 12, 86
lee, 15, 99
lucy, 11, 58
joseph, 19, 56
第一欄為姓名(name),第二欄為年紀(age),第三欄為得分(score)
現在,寫乙個python程式,
1)讀取檔案
2)列印如下結果:
得分低於60的人都有誰?
誰的名字以l開頭?
所有人的總分是多少?
3)姓名的首字母需要大寫,該record.txt是否符合此要求? 如何糾正錯誤的地方?
# define a class student, including name, age, score three properties
class student(object):
def __init__(self, name, age, score):
super(student, self).__init__()
self.name = name
self.age = age
self.score = score
f = open('record.txt', 'r')
students =
for content in f :
if not content.startswith('#'):
if not content.isspace():
line = content.strip().split(', ')
s = student(line[0], line[1], int(line[2] ))
f.close()
# the score is < 60
for s in students:
if s.score < 60:
print s.name, s.age, s.score
# name start with l
for s in students:
if s.name.startswith('l'):
print s.name, s.age, s.score
# the total score
total_score = 0
for s in students:
total_score += s.score
print total_score
wrong_name =
# is wrong?
for s in students:
if not s.name.istitle():
print 'the wrong data :', s.name, s.age, s.score
# eventually used to replace all files, documents directly to rewrite it again
def modify():
fr = open('record.txt', 'r')
line =
for content in fr:
if not content.startswith('#'):
if content.islower():
content = content[0].upper() + content[1:]
fr.close()
fw = open('record.txt', 'w')
fw.write(''.join(line))
fw.close()
modify()
Python小題目 針對快速教程
作業的目的是幫助熟悉之前學習的內容 1.寫乙個程式,判斷2008年是否是閏年。寫乙個程式,用於計算2008年10月1日是這一年的第幾天?2008年1月1日是這一年的第一天 這些小題目是為了方便大家加深對python理解而設計的。2.有乙個record.txt的文件,內容如下 name,age,sco...
Python基礎,,小題目
1.列印1 100 的奇數和偶數和 sum 0 for i in range 1,100,2 print i sum sum i print 奇數和為 s sum sum 0 for i in range 0,101,2 print i sum sum i print 偶數和為 s sum 2.列印...
mysql之常見函式 幾道小題目
mysql之常見函式 幾道小題目 我就直接po 了 1.顯示系統時間 注 日期 時間 select now as 當前時間 2.查詢員工號,姓名,工資,以及工資提高百分之20 後的結果 new salary select employee id,last name,salary,salary 1 0...