Python练习实例(1~10答案)

文章目录

      • 第一题
      • 第二题
      • 第三题
      • 第四题
      • 第五题
      • 第六题
      • 第七题
      • 第八题
      • 第九题
      • 第十题

第一题

for i in range(1,5):
    for j in range(1,5):
        for k in range(1,5):
            if( i != k ) and (i != j) and (j != k):
                print i,j,k

第二题

error_num = 0
while True:
    username = input("请输入用户名:")
    password = input("请输入密码:")
    if username == 'seven' or username == 'alex' and password == '123':
        print("登录成功")
        break
    else:
        print("登录失败")
        error_num += 1
        if error_num == 3:
            exit()
        else:
            continue

第三题

sum = 0
for i in range(2,101):
    if i % 2 == 0:
       sum += i
    else:
       sum -= i
    print(i,sum)
print("sum:",sum)

第四题

方法1:傻逼到家的方法
sum1 =0
sum2 = 0
for i in range(1,100,2):
    sum1 += i
for j in range(2,100,2):
    sum2 +=j
sum3 = sum1 -sum2
print(sum3)
 
方法2:正常程序猿的方法
sum=0
for i in range(0,100):
    if i%2==0:
        sum-=i
    else:
        sum+=i
print(sum)

第五题

sr1 = "abcdefghijklmnopqrstuvwxyz"
sr2 = sr1.upper()
sr = sr1 + sr1 + sr2 + sr2
in_str = input("")
out_str = ""
for j in in_str:
    if j == " ":
        out_str = out_str + " "
        continue
    i = sr.find(j)
    if(i > -1):
        out_str = out_str + sr[i+3]
print(out_str)

第六题

s = "All that doth flow we cannot liquid name Or else would fire and water be the same;But that is liquid which is moist and wet Fire that property can never get. Then 'tis not cold that doth the fire put out But 'tis the wet that makes it die, no doubt. "
print("这首词总共有:" + str(len(s)) + "个字符串")
print("这首诗是否以All开头:",s.startswith('All'))
print("这首诗是否以That\'s all, folks!结尾:",s.endswith('That\'s all, folks!'))
print("第一次出现单词the的位置:",s.find(' the '))
print("最后一次出现单词the的位置:",s.rfind(' the '))
print("the在诗中出现的总次数:",s.count(' the '))
print("是否诗中出现的所有字符都是字母和数字:",s.isalnum())

第七题

i = 1
x = 4
while i <= 9:
    if i <= 5:
        print("*" * i)
    elif i > 5:
        print("*" * x)
        x -= 1
    i += 1

第八题

i = 1
x = 4
while i <= 9:
    if i <= 5:
        print(" " * x + "*" * i + "*" * (i - 1))
    elif i > 5:
        print(" " * (i - 5) + "*" * (x + 5) + "*" * (x + 4))
    x -= 1
    i += 1

第九题

while True:
    year = int(input(">>> "))
    if year % 400 == 0 or year % 4 == 0 and year % 100 != 0:
        print("yeah it is runnian")
    else:
        print("not runnian")

第十题

line = int(input("请输入梯形底边的整数: "))
shang = int(input("请输入梯形上边的整数: "))
 
for i in range(shang,line+1):
    for k in range(line-i):
        print(" ",end="")
    for j in range(i*2-1):
        print("*",end="")
    print("")

你可能感兴趣的:(Python练习实例(1~10答案))