Day2作业讲解

练习1:猜年龄,猜对显示正确,猜错显示“你太笨了”
age = 28
count = 1

while count <= 3:
    _age = int(input("Age:"))
    if  _age > age:
        print("Should be smaller..")
    elif _age < age:
        print("Should be bigger..")
    elif _age == age:
        print("Yes! you guessed right")
        break
    count += 1
else:
    print('你太笨了')


练习2:使用while循环输出1-10,除了7不打印
三种方法可以实现
count = 1
while count <= 10:
    if count == 7:
        pass
    else:
        print(count)
    count +=1


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


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



练习3:求1-100的和
count = 1
suma = 0
while count <=100:
    suma = suma + count     #累加运算
    count +=1
    print(suma)

练习4:输出1-100内的所有奇数
练习5:输出1-100内的所有偶数
count = 1
while count <=100:
    if count %2 == 0:       #能除尽为偶数,除不尽为奇数
        print(count)
    count +=1

练习6:求1-2+3-4+5...99的所有数的和
count = 1
suma = 0        #0+1-2+3...
while count <=99:
    if count %2 == 1:   #奇数
        suma = suma + count
    else:   #偶数
        suma = suma - count
    count +=1
print(suma)




练习7:用户登录(三次输错机会)且每次输错时显示剩余错误次数(提示:使用字符串格式化)
count = 1
username = 'byh'
password = 123

while count <=3:
    _username = input('Username:')
    _password = int(input('Password:'))
    if _username == username and _password == password:
        print("Welcome %s login!" %(username))
        break
    else:
        print('Sorry, username or passowrd error. Still try %d' %(3-count))
    count += 1




练习8:判断广告标语:含 "最 第一 稀缺 国家级" 的为不合法
gd = input("请输入你的广告标语:")
if '最' in gd or '第一' in gd or '稀缺' in gd or '国家级' in gd:
    print('广告标语不合法')
else:
    print('广告标语合法')





练习9:输入一个数,判断这个数是几位数
count = 0
number = int(input("Number:"))

while number >= 1:
    number //=10
    count +=1

print(count)



猜你喜欢

转载自www.cnblogs.com/byho/p/10559316.html