编程语言
首页 > 编程语言> > 18大数据 Python常用语法课堂练习标程

18大数据 Python常用语法课堂练习标程

作者:互联网

文章目录

奇偶判读(if else使用)

题目链接:

https://nuoyanli.com/contest/6/problem/1800在这里插入图片描述

参考代码:

n = int(input())
if n % 2 == 0:
    print('even')
else:
    print('odd')

成绩等级判断(if else 及其if elif使用)

题目链接:

https://nuoyanli.com/contest/6/problem/1801
在这里插入图片描述

参考代码:

score = int(input())
if(score>=90 and score <= 100):
    print("A")
elif(score>=80 and score <90):
    print("B")
elif (score>=70 and score <80):
    print("C")
elif (score <70 and score>=60):
    print("D")
elif (score <60 and score>=0):
    print("E")
else:
	print("Input error")

编程求解1+2+3+…+n(for或者while使用)

题目链接:

https://nuoyanli.com/contest/6/problem/1802
在这里插入图片描述

参考代码:

for实现

n = int(input())
ans = 0
for i in range(1, n+1):
    ans += i
print(ans)

while实现

n = int(input())
ans = 0
i = 1
while i != n+1:
    ans += i
    i += 1
print(ans)

不能被3整除的数(continue的使用)

题目链接:

https://nuoyanli.com/contest/6/problem/1803
在这里插入图片描述

参考代码:

n = int(input())
for i in range(4,n+1):
    if i % 3 == 0:
        continue
    else:
        print(i,end=' ')

前n个偶数(break和continue的使用)

题目链接:

https://nuoyanli.com/contest/6/problem/1804
在这里插入图片描述

参考代码:

n = int(input())
sum = 0
for i in range(2, 2*n+1):
    if sum == n:
        break
    if i % 2 == 1:
        continue
    if i == 2:
        print("2", end="")
    else:
        print(" %d" % i, end="")
    sum += 1

打印九九乘法表(for或者while的使用)

题目链接:

https://nuoyanli.com/contest/6/problem/1805
在这里插入图片描述

参考代码:

for实现:

for i in range(1,10):
    for j in range(1,i+1):
        if i != j:
            print("%d*%d=%d" % (i, j, i*j), end=" ")
        else:
            print("%d*%d=%d" % (i, j, i * j))

while实现:

i = 1
while i < 10:
    j = 1
    while i >= j:
        if i != j:
            print("%d*%d=%d" % (i, j, i*j), end=" ")
        else:
            print("%d*%d=%d" % (i, j, i * j))
        j += 1
    i += 1

打印n*n乘法表(无语法限制)

题目链接:

https://nuoyanli.com/contest/6/problem/1806
在这里插入图片描述

参考代码:

for实现:

n = int(input())
for i in range(1,n+1):
    for j in range(1,i+1):
        if i != j:
            print("%d*%d=%d" % (i, j, i*j), end=" ")
        else:
            print("%d*%d=%d" % (i, j, i * j))

while实现:

n = int(input())
i = 1
while i < n+1:
    j = 1
    while i >= j:
        if i != j:
            print("%d*%d=%d" % (i, j, i*j), end=" ")
        else:
            print("%d*%d=%d" % (i, j, i * j))
        j += 1
    i += 1

标签:题目,课堂练习,Python,18,代码,else,while,print,链接
来源: https://blog.csdn.net/nuoyanli/article/details/100780783