# Python练习题（持续更新中）

### 第一题

```#方法一
#从2开始计算
i = 2
#定义一个变量用于保存结果
sum=0
while i <= 100:
if i % 2 == 0:
sum = sum + i
else:
sum = sum - i
i += 1
print("2-3+4-5+6...+100=",sum)

#方法二
n=3
sum=2
while n<=100 :
#n对2取余
if n % 2 != 0:
sum = sum - n
n = n + 1
else:
sum = sum + n
n = n + 1
print("2-3+4-5+6...+100=",sum)```

### 第二题

1!等于 1

2!等于 1*2

3!等于 1*2*3

n!等于 1*2*3*...*n

```n = int(input("请输入一个非负的数字：")) # 负数不算阶乘
def factorial(n):
if n == 0:
return 1  # 0的阶乘是1
else:
return n * factorial(n - 1)

if __name__ == ‘__main__‘:
result=factorial(n)
print("{}的阶乘为:{}".format(n,result))```

### 第三题

```try:
score=float(input(‘请输入考试成绩：‘))
if score>=90:
print(‘A‘)
elif 80<=score<90:
print(‘B‘)
elif 70<=score<80:
print(‘C‘)
elif 60<=score<70:
print(‘D‘)
else:
print(‘E‘)
except Exception as e:
print(‘您输入有误！‘)```

### 第四题

```save_money = float(input("请输入你要存入银行的钱："))
print("你存了{}元到银行".format(save_money))
total_money = save_money * 2 # 定义变量用于保存总钱数
year = 1 # 定义变量用于记录年份
while save_money < total_money:
save_money *= (1 + 0.0352)
year += 1
print("定期利率为3.52%，需要{}年本金和利息才能翻一番！".format(year))```

### 第五题

```# 方法一  字符串函数调用
a = ["I","T","e","s","t","e","r"]
print("".join(a))

#方法二 for循环
a = ["I","T","e","s","t","e","r"]
s = ""
for item in a:
s += item
print(s)```

Python练习题（持续更新中）

(0)
(0)