答案:
# 1.使用while循环完成
i = 1
while i<=9:
j = 1
while j<=i:
print("%d x %d = %-2d\t"%(j,i,j*i),end="")
j+=1
i+=1
print()
print('='*50 + '华丽的分割线' + '='*50)
# 2.使用for循环完成
for i in range(1,10):
for j in range(1,i+1):
print("%d X %d = %-2d"%(j,i,i*j),end=' ')
print('')
答案:
print("练习1:判断奇偶")
num = input("请输入一个自然数:")
if num.isdigit():
num = int(num)
while num%2==0:
print("您输入的是一个偶数")
break
else:
print("您输入的是一个奇数")
else:
print("您输入的不是一个自然数")
结果:
答案:
nums = 0
plan_list = []
money = 100
for i in range(0,34):
money -= i*3
for j in range(0,money+1):
k = (money-j)*2
if i+j+k == 100:
nums += 1
plan = ["%d只母鸡"%i,"%d只公鸡"%j,"%d只小鸡"%k]
plan_list.append(plan)
print("共有%d种方法,分别是:"%nums,end="")
i = 0
for plan in plan_list:
i+=1
print('%d.'%i,plan[0],plan[1],plan[2],end=" ")
结果:
答案:
str_input = input("请输入一行非空字符串(长度小于5000),单词以空格隔开:")
len_lastwd = len(str_input.split()[-1])
print("最后一个单词的长度为:%d"%len_lastwd)
结果:
答案:
list1 = ['a','abc',1,(1,'a')]
list2 = [34,'abc',1,True]
list_intersection = [value for value in list1 if value in list2]
list3 = [value for value in list1+list2 if value not in list_intersection]
print("列表1为:{}\n列表2为:{}\n不相同的元素为:{}".format(list1,list2,list3))
结果:
答案:
my_list = [1,2,'abc',[1,'a'],{1,2,'a'}]
for index,value in enumerate(my_list):
if index % 2 == 1:
print(index,value)
结果:
答案:
def fibonacci(num):
if num == 1 or num == 2:
return 1
else:
return fibonacci(num-1)+fibonacci(num-2)
num = int(input('请输入需要的斐波那契数列长度:'))
print([fibonacci(i) for i in range(1,num+1)])
结果: