#输出 a = int(input('请输入一个值:')) #阶乘控制单位 total = 1 #求阶乘 for i in range(1,a+1,1): ##起始值为1,终止符为a+1,间隔为1求阶乘 total *= i print(total) 12345678
练习求1,2,3,4四个数字可以组成多少个不同的且不重复的三位数字
""" file:求1,2,3,4四个数可以组成多少个不重复且不相同的三位数字并打印 date:2019-6-23 09:46 description: """ #不相同数字的数目 num = 0 #对三位数字取值 for g in range(1,5,1): for s in range(1,5,1): for b in range(1, 5, 1): #判断三个数字是否有相同数字,若没有则打印且num自加1 if(g != s and g != b and b != s ): print('%d%d%d' %(b,s,g)) num += 1 #输出有多少个不重复数字 print('共%d个不重复且不相同的数字' %(num)) 1234567891011121314151617 在这里插入代码片 1
for循环实现用户登录 """ for i in range(3): name = input('用户名:') passwd = input('密码:') if ((name == 'root') and (passwd == 'westos')): print('登陆成功') break else: print('登陆失败') print('您还有%d次机会' %(2-i))
import os for i in range(1000): cmd = input('[root@python ~]') if cmd: if cmd == 'exit': print('logout') else: os.system(cmd) else: continue 12345678910
求最大公约数与最小公倍数
a = int(input('输入第一个数:')) b = int(input('输入第二个数:')) small = min(a,b) big = max(a,b) sum = 1 for i in range(1,small+1): if (a % i == 0) and (b % i == 0): sum = 1 * i continue
eg: #1.定义计数器 i = 0 #2.条件判断 while i < 3: #3.处理计数器 i = i + 1 print("%d" %(i)) 12345678
while实现for循环编辑的简易用户登录
i = 0 while i <= 2: name = input('用户名:') passwd = input('密码:') if ((name == 'root') and (passwd == 'westos')): print('登陆成功') break else: print('登陆失败') print('您还有%d次机会' %(2-i)) i = i + 1 else: print('失败超过三次,请稍后再试') 12345678910111213
while打印四种直角三角形 第一行输出1颗星,每行依次加一颗星,直至第五行 第一种
row = 1
while row <= 5: col = 1 while col <= row: print('*',end='') col += 1 print('') row += 1 123456789
第二种 倒序输出上题条件
row = 1
while row <= 5: col = 5 while col >= row: print('*',end='') col -= 1 print('') row += 1 123456789
第三种
row = 1 while row <= 5: col = 1 while col < row: print(' ',end='') col += 1 while col >= row and col <= 5: print('*',end='') col += 1 print('') row += 1 1234567891011
第四种
row = 1 while row <= 5: col = 1 while col <= (5 - row): print(' ',end='') col += 1 while (col >(5-row) and col <=5): print('*',end='') col += 1 print('') row += 1 1234567891011
打印乘法口诀表
row = 1 while row <= 9: col = 1 while col <= row: print('%d * %d = %d ' %(col,row,col*row),end='') col += 1 print('') row += 1 12345678
import random num = 0 c = int(random.randint(1, 100)) while True and num <=4 : print('您还有%d次机会' % (5 - num)) i = int(input('请输入数字:')) if i == c: print('congratulation') break elif i > c: print('too big') num += 1 else: print('too small') num += 1 if num == 5: print('数字为%d,你猜错啦' %(c))
123456789101112131415161718
字符串定义 定义: a = ‘hello’ b = “westos” c = ‘let’s go’ d = “let’s go” e = “”” 1.打印 2.复制 3.copy “”” 字符串特性 ###索引 #s = ‘hello’ #print(s[0]) #print(s[1]) #print(s[2])