知识点1:
输入变为浮点数:score = input("请输入成绩:")
score = float(score)
1题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
"""
File: 4学习成绩等级.py
Author: chde_wang
Date: 2020-07-22-23-16
Description:判断学生成绩等级
"""
score = input("请输入成绩:")
grade = ""
try:
score = float(score)
if score > 100:
print("您输入的成绩不合法!")
elif score >= 90:
grade = "A"
print('该成绩等级为:', grade)
# elif score >= 60 and score <= 89:
elif 60 <= score <= 89:
grade = "B"
print('该成绩等级为:', grade)
else:
grade = "C"
print('该成绩等级为:', grade)
except ValueError:
print("您输入的不是数字!")
没有考虑分数小于0:,修改:
"""
File: 4学习成绩等级.py
Author: chde_wang
Date: 2020-07-22-23-16
Description:判断学生成绩等级
"""
score = input("请输入成绩:")
grade = ""
try:
score = float(score)
if score > 100 or score < 0:
print("您输入的成绩不合法!")
elif score >= 90:
grade = "A"
print('该成绩等级为:', grade)
# elif score >= 60 and score <= 89:
elif 60 <= score <= 89:
grade = "B"
print('该成绩等级为:', grade)
else:
grade = "C"
print('该成绩等级为:', grade)
except ValueError:
print("您输入的不是数字!")
2题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
"""
File: 统计个数.py
Author: chde_wang
Date: 2020-07-22
Description:统计字母、数字、空格、其他个数
"""
s = input()
sum_character = 0
sum_space = 0
sum_num = 0
sum_other = 0
# 求ASCII码 ord()
# 转字符 chr()
for i in s:
i = ord(i)
if (65 <= i <= 97) or (97 <= i <= 122): # 大小写
sum_character = sum_character+1
elif 48 <= i <= 57:
sum_num = sum_num+1
elif i == 32:
sum_space = sum_space+1
else:
sum_other = sum_other+1
print("sum_character:", sum_character)
print("sum_num", sum_num)
print("sum_space", sum_space)
print("sum_other", sum_other)
3题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
"""
File: 1乒乓球弹跳.py
Author: chde_wang
Date: 2020-07-20
Description:一球从100米高度自由落下,每次落地后反跳回原高度的一半,
再落下,求它在第10次落地时,共经过多少米,第10次反弹多高?
"""
# 原思路
'''
height=100
sum_height_lst=[]
for i in range(10):
sum_height_lst.append(height) 只弹跳了9次
height=height/2
sum_height=0
for i in sum_height_lst:
print(i)
sum_height=sum_height+i
print(sum_height)
print(sum_height_lst[-1])
'''
# 仔细考虑之后,从第二次之后开始弹跳 总高度为弹跳的二倍
height = 100
sum_height_lst = []
for i in range(11):
sum_height_lst.append(height)
height = height/2
sum_height = 0
for i in range(len(sum_height_lst)):
print(sum_height_lst[i])
for i in range(len(sum_height_lst)):
print(sum_height_lst[i])
if i == 0:
sum_height = sum_height_lst[i]+sum_height
print("第", i, "次,", "高度为:", sum_height)
else:
sum_height = sum_height_lst[i]*2 + sum_height
print("第", i, "次,", "高度为:", sum_height)
print(sum_height)
print(sum_height_lst[-1])
修改:
# 仔细考虑之后,从第二次之后开始弹跳 总高度为弹跳的二倍
# 第一次的球经过的高度为100
# 从第二次后 球经过的高度为 弹跳距离的二倍
# 球第二次经过的高度为球的第一次弹跳
# 所以球第十次经过高度后为 球的第9次弹跳
# 所以还需要再弹跳一次
height = 100
sum_height_lst = []
for i in range(10):
sum_height_lst.append(height)
height = height/2
sum_height = 0
for i in range(len(sum_height_lst)):
if i == 0:
sum_height = sum_height_lst[i]+sum_height
print("第", i+1, "次,", "弹跳高度为:", sum_height_lst[i], "总高度为:", sum_height)
else:
sum_height = sum_height_lst[i]*2 + sum_height
print("第", i+1, "次,", "弹跳高度为:", sum_height_lst[i], "总高度为:", sum_height)
print(sum_height)
print(sum_height_lst[-1]/2)
网上代码:
# -*- coding: utf-8 -*-
far = []
high = 100
for i in range(1, 11):
if i == 1:
far.append(high)
else:
far.append(high * 2)
high = high / 2
print(f'经过的总距离:far = {sum(far)}')
print(f'第十次反弹多高:high = {high}')
# --------------------------------------------------------
# 经过的总距离:far = 299.609375
# 第十次反弹多高:high = 0.09765625
4题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
"""
File: 2比赛对抗.py
Author: chde_wang
Date: 2020-07-21
Description: a不和x比赛,c不和x、z比赛
"""
import copy
list_jia = ['a', 'b', 'c']
list_yi = ['x', 'y', 'z']
list_a = copy.copy(list_yi)
list_b = copy.copy(list_yi)
list_c = copy.copy(list_yi)
# list_a=list_b=list_c=list_yi # 此处为深拷贝(拷贝地址)
for i in list_jia:
for j in list_yi:
if i == 'a' and j == 'x':
list_a.remove('x') # pop(index) 若没有index则默认为列表最后一个
elif i == 'c':
if j == 'x':
list_c.remove('x')
elif j == 'z':
list_c.remove('z')
while len(list_yi) != 0:
if len(list_a) == 1:
print("a:", list_a[0])
list_yi.remove(list_a[0])
if list_a[0] in list_b:
list_b.remove(list_a[0])
if list_a[0] in list_c:
list_c.remove(list_a[0])
list_a.pop()
elif len(list_b) == 1:
print("b:", list_b[0])
list_yi.remove(list_b[0])
if list_b[0] in list_a:
list_a.remove(list_b[0])
if list_b[0] in list_c:
list_c.remove(list_b[0])
list_b.pop()
elif len(list_c) == 1:
print("c:", list_c[0])
list_yi.remove(list_c[0])
if list_c[0] in list_a:
list_a.remove(list_c[0])
if list_c[0] in list_b:
list_b.remove(list_c[0])
list_c.pop()
5题目:打印出如下图案(菱形):
*
***
*****
*******
*****
***
*
'''
打印菱形
前半部分和后半部分相同 倒序输出即可
考虑空格数和星号数的关系
'''
len_lx = 7 # 菱形宽度
for i in range(1, len_lx, 2):
len_space = (len_lx-i) // 2 # /为除法 //为整除 %取模
print(' '*len_space, end='')
print('*'*i, end='')
print(' '*len_space)
for i in range(len_lx,0,-2):
len_space = (len_lx - i) // 2
print(' ' * len_space, end='')
print('*' * i, end='')
print(' ' * len_space)