python练习题
1、使⽤while循环输出 1 2 3 4 5 6 8 9 10
count = 1
while count <= 10 :
if count == 7 :
count +=1
continue
print(count,end=" ")
count +=1
num = 1
while num <= 10 :
print(num,end=" ")
num += 1 #num + 1
2、求1-100的所有数的和
num = 1
h = 0
while num <= 100 :
h = h + num
num += 1
print(h)
3、输出 1-100 内的所有奇数
num = 1
while num <= 100 :
if num % 2 == 1:
print(num,end=" ")
else:
pass
num +=1
4、输出 1-100 内的所有偶数
num = 1
while num <= 100 :
if num % 2 == 0:
print(num,end=" ")
else:
pass
num +=1
5、求1-2+3-4+5 ... 99的所有数的和
num = 1
h = 0
while num < 100 :
if num % 2 == 1:
h = h + num
else :
h = h - num
num +=1
print(h)
6、用户登陆(三次机会重试)
num = 1
name = "admin"
print("您有三次输入机会!")
while num <= 3:
user = input("请输入用户名:")
if user == name :
print("登录成功!")
elif num == 3:
print("您的输入次数已用完,请确认用户名是否正确")
break
else:
print("用户名不正确,请确认用户名是否正确")
num += 1
PS:本文仅限个人练习记录,代码不具有任何代表性
仅供参考,欢迎指正