一、输入某年某月某日,判断这一天是这一年的第几天?
year,month,day = eval(input("请输入年月日"))
m1 = [0,31,59,90,120,151,181,212,243,271,304,334,365]
m2 = [0,31,60,91,121,152,182,213,244,275,305,335,366]
if year %4 == 0 and year % 100 != 0 or year % 400 == 0:
sum = m2[month-1] + day;
else:
sum = m1[month-1] + day;
print("this is the ",sum)
OR
year,month,day = eval(input("请输入年,月,日"))
DaysOfMonth = [31,28,31,30,31,30,31,31,30,31,30,31]
dayseq=0
for m in range(month-1):
dayseq += DaysOfMonth[m]
dayseq+=day
if month > 2 and ((year % 4 == 0 and year % 100 != 0) or year % 400 == 0):
dayseq+=1
template='%d-%d-%d是本年度的第%d天'
result = (year,month,day,dayseq)
print(template%result)
二、 将一个正整数分解质因数。例如:输入90,打印出90=233*5。
num = int(input('Enter a number'))
n=num
result=[]
while n >1:
for i in range(n-1):
k=i+2
if n%k==0:
result.append(k)
n=n//k
break
print(result)
r = str(num)+'='
for k in result:
r+=str(k)+'*'
r=r[:-1]
print(r)
三、学习成绩转为级别表示法
g = int(input("请输入成绩\n"))
if g >= 90:
print('A')
elif g >= 80:
print('B')
elif g >= 70:
print('C')
elif g >= 60:
print('D')
else:
print('E')
四、输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
s = input("Please enter a character string\n")
letters = 0
space = 0
digit = 0
others = 0
for c in s:
if ord(c) in range(65,91):
letters += 1
elif ord(c) in range(97,123):
letters += 1
elif ord(c)==32:
space += 1
elif ord(c) in range(48,58):
digit += 1
else:
others += 1
print("char=%d space=%d digit=%d others=%d"%(letters,space,digit,others))
五、打印九九乘法表(对齐,嵌套循环)end函数
for j in range(1,10):
for k in range(1,10):
if j >= k:
print("%d*%d=%d\t"%(k,j,j*k),end='')
else:
print("\t",end='')
print()
六、一首古诗横向(左右上下)输出和竖向(上下右左)输出(二维列表使用)
num_list = [['世', '人', '万', '千'], ['再', '难', '遇', '我']]
num_list2 = [['床', '前', '明', '月', '光'], ['疑', '是', '地', '上', '霜']]
lin = 5
row = 2
for j in range(0, row):
for k in range(0, lin):
print(num_list2[j][k], end='')
print()
print()
lin = 4
for k in range(0, lin):
for j in range(row-1, -1,-1):
print(num_list[j][k], end='')
print()
---------------------------
床前明月光
疑是地上霜
再世
难人
遇万
我千
七、模拟c。首先显示起点到目标点的车次及余票等信息,根据用户的选择,成功出票
def time_information(a,b):
j=0
for i in range(6,22,2):
print('%d %d:30(上海虹桥) ->%d:30(北京南)'%(j,i,i))
j+=1
def ticket_information(a,b):
if a == '上海' and b == '北京':
time_information(a,b)
def choice_ticket(n):
print(year,month,day)
print('%d:30(上海虹桥) ->%d:30(北京南)' %(n,n))
print('success!!')
departure,destination=eval(input('Please enter the place of departure and destination:\n'))
year,month,day = eval(input('Please enter the start day 例:2000,5,7'))
ticket_information(departure,destination)#'上海','北京'
n=int(input('Please choice the ticket'))
choice_ticket(n)
book = []
def show_Menu():
print('========图书管理系统========')
print('1.增加图书和价格\n2.删除图书\n3.修改价格\n4.查询所有图书\n5.根据图书查找价格\n6.退出')
print('==========================')
n=int(input('Enter your choice'))
return n
def Add_book_price():
b_name,b_price= input("Enter the book's name and price").split()
book.append([b_name,b_price])
def delete_book():
i = find_book()
m = input('Do you want to delete this book?\nyes no\n')
if m=='yes':
book.remove(i)
print('delete success')
else:
print('cancel success')
def revise_price():
i = find_book()
new_price = input('Please enter the new price:')
i[1] = new_price
def show_all_book():
for i in book:
print('《' + i[0] + '》\t', i[1] + '元')
def find_book():
b_name = input("Enter the book's name")
ans = 0
for i in book:
if b_name == i[0]:
ans = 1
print('《' + i[0] + '》\t', i[1] + '元')
return i
if ans == 0:
print("查无此书!")
while (1):
t = show_Menu()
if t == 1:
Add_book_price()
elif t==2:
delete_book()
elif t==3:
revise_price()
elif t==4:
show_all_book()
elif t==5:
find_book()
else:
print('See you!')
break