hello world

人生若只如初见

小练习1

  • 猜大小
count = 48
while True:
    num = int(input('please enter number'))
    if num == count:
        print('successfully')
        break
    elif num >count:
        print('enter number big')
    else:
        print('enter number little')
  • 允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
count = 0
while count <3:
    username = input('please enter your name:')
    password = input('please enter your PIN:')
    if username == 'xiaoming' and password == '123word':
        print('welcome')
        break
    else:
        print('please name or password')
    count += 1
  • 升级版
  1. 允许用户最多尝试3次
  2. 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
  3. 如何猜对了,就直接退出
count = 1
while count <=3:
    username = input('please enter your name')
    password = input('please enter you PIN')
    if username == 'xiaoming' and password == 'word123':
        print('successfully login')
        break
    else:
        print('login failed')
        if count == 3:
            num = input('please Y/N')
            if num == 'N':
                break
            elif num == 'Y':
                count = 1
                continue
            else:
                print('enter error')
            break
    count += 1
  • a. 使用while循环实现输出2-3+4-5+6...+100 的和
total = 0
count  = 2
while count <=100:
    if count%2 == 0:
        total += count
    elif count%2 == 1:
        total -= count
    count += 1
print(total)
  • b. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12
count = 0
while count<=12:
    if count  == 6 or count == 10 :
        pass
    else:
        print(count)
    count += 1
  • c. 使用 while 循环实现输出 1-100 内的所有奇数
count = 1
while count<100:
    if count %2 == 1:
        print(count)
    count+=1
  • d. 使用 while 循环实现输出 1-100 内的所有偶数
count = 1
while count<101:
    if count %2 == 0:
        print(count)
    count+=1
  • e.使用while 循环输出100-50,从大到小,如100,99,98…,到50时再从0循环输出到50,然后结束
count = 101
while count >0:
    if count>50:
        count -=1
        print(count)
    elif count == 50:
        count = 0
        while count <50:
            count+=1
            print(count)
        break
  • 使用while,完成以下图形的输出
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
count = 1
b = 5
while count<5:
    print(count*'* ')
    count += 1
while b>0:
     print(b*'* ')
     b -= 1

posted on 2019-08-26 15:32  一壶兔  阅读(252)  评论(0编辑  收藏  举报

导航