目录
- 前言
- 附录
前言
本次内容主要围绕之前介绍的内容和方法,列举出相应练习题,在附录上面有相应的代码。当然这些代码还可以进一步进行优化,有想法的朋友,可以私信发给我更好更完善的代码。
附录
成绩判断.py
grade = int(input("你的考试成绩:"))
if grade < 100:
if grade > 0:
print("你输入的成绩有效")
else:
print("你输入的成绩无效")
else:
print("你输入的成绩无效")
if grade > 90:
print("你是A类")
elif 80 < grade < 90:
print("你是B类")
elif 70 < grade < 80:
print("你是C类")
elif 60 < grade < 70:
print("你是D类")
else:
print("你不及格")
给出数字求和_for.py
n = int(input('How many numbers to sum?'))
total = 0
for i in range(n):
s = input('Enter number '+ str(i+1) +': ')
total += int(s)
print('The sum is '+str(total))
给出数字求和_while.py
n = int(input('How many numbers to sum?'))
total = 0
i = 1
while i <= n:
s =input('Enter number '+ str(i) +': ')
total += int(s)
i += 1
print('The sum is '+str(total))
九九乘法表.py
for row in range(1,10):
for col in range(1,10):
prod = row * col
if prod < 10:
print(' ',end='')
print(row * col,' ',end='')
print()
九九乘法表_改进.py
for row in range(1,10):
for col in range(1,row+1):
prod = row * col
if prod < 10:
print(' ',end='')
print(row * col,' ',end='')
print()
1到100奇数和.py
s = 0
for i in range(1,101,2):
s = s+i
print("sum=",s)
1到100偶数和.py
s = 0
for i in range(2,101,2):
s = s+i
print("sum=",s)
求阶乘_for.py
fact = 1
n = int(input('请输入要求的整数的阶乘:'))
if n > 0:
print("输入正确正在计算")
elif n == 0:
print('阶乘是'+str(fact))
else:
print("输入有误请重新输入")
while n < 0:
break
for i in range(2,n+1):
fact = fact * i
print(str(n)+'阶乘是'+str(fact))
求阶乘_while.py
n = int(input('请输入要求数的阶乘:'))
fact = 1
i = 2
while i <= n:
fact = fact * i
i = i + 1
print(str(n)+'阶乘是'+str(fact))
未知个数数字求和.py
s = int(input('Enter a number (or "done"): '))
total = 0
while s != 'done':
num = int(s)
total += num
s = (input('Enter a number (or "done"): '))
print('The sum is ' + str(total))