Python入门 Day1-小练习

 1. 使用while循环输入 1 2 3 4 5 6 8 9 10

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

  

2. 求1-100的所有数的和

#f方法1:
i = 1
sum = 0
while i < 101:
    sum += i
    i+=1
print(sum)

#方法2:
print(sum(range(1,101,1)))

 

 

3. 输出 1-100 内的所有奇数

#方法1:
i = 1
while i <= 100:
    print(i)
    i += 2
# 方法2:
i = 1
while i <= 100:
    if i % 2 == 1:
        print(i)
    i += 1
##方法3:
for i in range(1,100,2):
    print(i)

 

 

4. 输出 1-100 内的所有偶数

#方法1:
i = 1
while i <= 100:
    if i % 2 == 0:
        print(i)
    i += 1
##方法2
for i in range(0,101,2):
    print(i)

 

 

5. 求1-2+3-4+5 ... 99的所有数的和

#方法1:
i = 1
sum = 0
while i < 100:
    if i % 2 == 1:
        sum += i
    else:
        sum -= i
    i += 1
print(sum)

# #方法2:
i = 1
sum = 0
sub = 0
while i < 100:
    if i % 2 == 1:
        sum += i
    else:
        sub -= i
    i += 1
print(sum+sub)

#方法3:
a = sum(range(1,100,2))
b = sum(range(0,100,2))
print(a-b)

 

 

6. 登录程序

    要求:可以支持多用户登录,登录三次失败后要求确认是否继续验证

input  username password

li = [{'username':'alex','password':'SB'},
    {'username':'wusir','password':'sb'},
    {'username':'taibai','password':'nanshen'}
    ]

cot = 1

    # print(i['username'],i['password'])
while cot <= 3:
    Username = input('Your username: ')
    Password = input('Your password:')
    for i in li:
        #print(i['username'],i['password'])
        if Username == i['username'] and Password == i['password']:
            print('access success')
            cot = 4
            break
    else:
        print('Your username or password is wrong')
        cot += 1
        if cot == 4:
            con = input('错太多次了,Do you want to continue:')
            if con == 'Y' or con == 'y':
                cot = 1
            else:
                print('access denied')
                break
#

 

再默写一个吧

##  在默写一遍

##########################################################################
li = [{'username':'alex','password':'SB'},
    {'username':'wusir','password':'sb'},
    {'username':'taibai','password':'nanshen'}
    ]
cot = 1
while cot <= 3:
    Usernm = input('Username:')
    Passwd = input('Password:')
    for i in li:
        if Usernm == i['username'] and Passwd == i['password']:
            print('Welcome',i['username'])
            cot = 4
            break
    else:
        cot += 1
        if cot == 4:
            Asw = input('错太多次了, Try again(Y|N)?')
            if Asw == 'Y' or Asw == 'y':
                cot = 1
            else:
                print("这么多次也不对?拜拜喽")
        else:
            print('Your username or password is wrong,Try again')

 

posted on 2018-04-03 11:54  鸿飞漫天  阅读(178)  评论(0编辑  收藏  举报

导航