python学习第1天

练习题

练习1

使用while循坏输出1 2 3 4 5 6  8 9 10

方法1

count = 1
while count < 11:
    if count != 7:
        print(count)
    count += 1

 方法2

count = 0
while count < 10:
    count += 1
    if count == 7:
        continue
    print(count)

 方法3

count = 1
while count < 11:
    if count == 7:
        pass
    else:
        print(count)
    count = count +1
print()

练习2
求1+2+3+.....+100的和

方法1

sum = 0
count = 1
while count < 101:
    sum = sum + count
    count += 1
print(sum)

 方法2

count = 1
s = 0
while count < 101:
    s = s + count
    count = count + 1
print(s)

练习3
求1--100的所有奇数

方法1

count = 1
s = 0
while count < 101:
    s = s + count
    count = count + 1
print(s)

方法2

count = 1
while count < 101:
    temp = count % 2
    if temp == 0:
        pass
    else:
        print(count)
    count = count + 1
print()

 

练习4
求1--100的所有偶数

方法1

count = 1
while count < 101:
    if count % 2 == 0:
        print(count)
    count += 1

 方法2

count = 1
while count < 101:
    temp = count % 2
    if temp == 0:
        print (count)
    else:
        pass
    count = count + 1
print ()

练习5
求1-2+3-4+5....+99的和


方法1

sum1 = 0
sum2 = 0
count = 1
while count < 100:
    if count % 2 != 0:
        sum1 = sum1 + count
    else:
        sum2 = sum2 - count
    count += 1
print(sum1 + sum2)

 方法2

num = 1
num2 = 2
num3 = num - num2
while True:
    num += 2
    num2 += 2
    num3 =num + num3
    if num == 99:
        break
    num3 = num3 - num2
print(num3)

 方法3

sum = 1
s = 0
while True:
    s = s +sum
    if sum == 99:
        break
    sum += 2
sum1 = 0
s1 = 0
while True:
    s1 = s1 + sum1
    if sum1 == 98:
        break
    sum1 += 2
print(s - s1)

方法4

sum = 0
count = 1
while start < 100:
    temp = start % 2
    if temp == 1:
        sum =sum + count
    else:
        sum =sum - count
    count += 1
print(sum)

方法5

sum = 1
s = 0
while sum < 100:
    s = s + sum*(-1)**(sum + 1)
    sum += 1
print(s)

方法6

print(49*(1 - 2)+99)

练习6
用户登录(三次机会重试)

基本的功能:

user ='script_child'
pwd ='123456'
count = 1
while True:
    if count == 4:
        print('Too many times!!')
        break
    username = input('Please enter your user name:').strip()    
    password = input('Please enter your user password:')
    if username == user and password == pwd:
        print('Login successfully!!')
    else:
        print('Login failed!!')
    count += 1   

去除bug,添加一些功能:

                           添加限制:当用户连续输入错误次数超过三次,就锁定这个用户

user = 'script_child'
pwd = '123456'
count = 1
while True:
    if count == 4:
        print('Too many times!')
        break
    username = input('Please enter your username:').strip()
    password = input('Please enter your password:')
    if not username:
        print('The username you entered is blank, please re-enter')
        continue
    elif not password:
        print('The password you entered is blank, please re-enter')
        continue
    if username == user and password == pwd:
        print('Login successfully!!')
        break
    else:
        print('The username or password you entered is incorrect,Please try again!')
        print('You still have %s chances'%(3-count))
    count += 1

再添加一些功能:

                      要求:1、用户输入账号或密码为空时,让用户反复输入并提示让其输入用户名和密码

                                 2、每登陆一次失败后就告之用户还有几次机会       

                                 3、用户登陆成功后让用户输入命令,用户输入什么命令就打印这个命令,如果用户输入的命令为“q  则退出整个程序

 

user = 'script_child'
pwd = '123'
tag = True
count = 1
while tag:
    if count == 4:
        print('Too many times!')
        break
    username = input('Please enter your username:').strip()
    password = input('Please enter your password:')
    if not username:
        print('The username you entered is blank,please re-enter')
        continue
    elif not password:
        print('The password you entered is blank, please re-enter')
        continue
    if username == user and password == pwd:
        print('Login successfully!!')
        while tag:
            user_cmd = input('Please enter your command:').strip()
            if user_cmd == 'q':
                tag = False
                break
            print('Your command is %s' % user_cmd)
    else:
        print('The username or password you entered is incorrect,Please try again!')
        print('You still have %s chances'%(3-count))

        count += 1

 

  

 

 

 

 

 



posted @ 2018-01-22 22:08  脚本小孩  阅读(386)  评论(0编辑  收藏  举报