新手python小白练习题

如题所述


新手python小白练习题

初学者练习

1 跟着步骤写一遍

2 自己练习写一遍

举一反三

1 根据习题思路,设计类似题目

2 自己执行

设计“过7游戏”的程序,打印I-100之间除了含7和7的倍数以外的数字代码展示:

foriin range(101):ifi%7!=0:print(i,end="")

运行结果

1 23 4 5 6 8 9 10 11 12 13 15 16 17 18 19 20 22 23 24 25 26 27 29303132 33 34 36 37 38 39 40 41 3 4 45 46 47 8 50 5152 53 5455 57 58 59 60 6162 64 65 66 67 68 69 7172 73 74 75 76 78 79 808182 83 85 86 87 88 89 90 92 93 94 95 96 97 99 100

编写程序用户登录程序且仅有三次 (

机会(f和for)

代码展示:name= admin"password="123456"i=3

whilei>0:

iname=input(请输入用户名:")ipassword=input("请输入用户密码")ifiname==nameandipassword==password:print(“登录成功")breakelse:print("登录失败,请重新输入!")i-=1

else:

print(“您已经失败三次,无法登录了!")

运行结果:

请输入用户名:admin请输入用户密码123456登录成功

编写程序实现,一串字符串是否为有效变量名

代码展示:strl=input("请输入一串字符:")if'instr1:

str2=strl.replace( '-,a )ifstr2.isalnum():if str2[o].isdigit():print("该字符为非法变量名")else:

print(“该字符为合法变量名")

else:

print("该字符为非法变量名”)elif 'notinstr1:ifstrl.isalnum():if str1[o].isdigit():print("该字符为非法变量名")else:

print("该字符为合法变量名")

else:print("该字符为非法变量名")运行结果:

请输入一串字符:_.;该字符为非法变量名

使用while循环实现2-3+4-5+6..+100的和。代码展示:

num=2count=0while num <=100:if num%2==0:

count=count+numelse:count=count-numnum+=1print(count)

运行结果:

51

使用循环实现九九乘法表

代码展示:

for i in range(1,10):for xin range(1,i+ 1):print('%d X %d =%d'% (i, x, i* x),end=')print('n')

运行结果:

1X1=1

2X1=22X2=4

3X1=33X2=63X3=9

4X1=44X2=84X3=124X4=16

5X1=55X2=105X3=155X4=205X5=256X1=66X2=126X3=18 6X4=246X5=306X6=367X1=77X2=147X3=217X4=287X5=357X6=427X7=498X1=88X2=168X3=248X4=328X5=408X6=488X7=568X8=649X1=99X2=189X3=279X4=369X5=459X6=549X7=639X8=729X9=81

已知列表 i=

[22478.24066.23398,384981利用字符串拼接遍历,输出结果“城市学院”

代码展示:

li=[2 2 4 7 8 , 2 4 0 6 6 , 2 3 3 9 8 3 849 8]str1=foriin li:strl=strl+chr(i)print(str1)

运行结果:

城市学院

温馨提示:答案为网友推荐,仅供参考
大家正在搜