python 练习1(流控制)

#!/usr/bin/python
#_*_ coding:utf-8 _*_


#练习题
#1、使用while循环输入 1 2 3 4 5 6 8 9 10
#a.定义一个变量存放数字
#b.用while判断大于10结束
test1=1
while test1<=10:
print(test1)
test1+=1

#2、求1-100的所有数的和
#a.定义一个变量存放和,另外一个变量存放数字
#b.while 循环加
test2=1
count1=0
while test2<=100:
count1=count1+test2
test2 +=1
print(count1)

#3、输出 1-100 内的所有奇数
#a.定义一个变量存放数字
#b.while 循环取2的余数,如果不为0是奇数
test3=1
while test3<=100:
tf=test3%2
if tf==0:
pass
else:
print(test3)
test3 +=1

#4、输出 1-100 内的所有偶数
test4=1
while test4<=100:
tf=test4%2
if tf==0:
print(test4)
test4 +=1

#5、求1-2+3-4+5 ... 99的所有数的和
#a.判断数字寄数用加号,是偶数用减号
#b.输了等式和结果
test5=2
stest='1'
count2=1
while test5<=99:
tf=test5%2
if tf == 0:
count2=count2 + test5
stest=stest+'-'+test5.__str__()
else:
count2=count2 - test5
stest=stest+'+'+test5.__str__()
test5 +=1

print('{0}={1}'.format(stest,count2))
 
#6、用户登陆(三次机会重试)
#a.定义三个变量
#b.获取用户输入名和密码
#c.判断是否相等,记不等次数

import getpass
name=''
passwd=''
n=1
while n<=3:
name=input('请输入用户名:')
passwd=getpass.getpass('请输入密码:')
if name== 'yjy' and passwd == '123':
print('恭喜你,登录成功!')
break
else:
pass
n +=1
原文地址:https://www.cnblogs.com/tonyjy/p/8621306.html