循环结构

第1关 While 循环与 break 语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
partcount = int(input())
electric = int(input())
count = 0
#请在此添加代码,当count < partcount时的while循环判断语句
#********** Begin *********#
while count < partcount:
#********** End **********#
    count += 1
    print("已加工零件个数:",count)
    if(electric):
        print("停电了,停止加工")
        #请在此添加代码,填入break语句
        #********** Begin *********#
        break
        #********** End **********#

第2关 for 循环与 continue 语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
absencenum = int(input())
studentname = []
inputlist = input()
for i in inputlist.split(','):
   result = i
   studentname.append(result)
count = 0
#请在此添加代码,填入循环遍历studentname列表的代码
#********** Begin *********#
for student in studentname:
#********** End **********#
    count += 1
    if(count == absencenum):
        #在下面填入continue语句
        #********** Begin *********#
        continue
        #********** End **********#
    print(student,"的试卷已阅")

第3关 循环嵌套

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
studentnum = int(input())

#请在此添加代码,填入for循环遍历学生人数的代码
#********** Begin *********#
for student in range(studentnum):
#********** End **********#
    sum = 0
    subjectscore = []
    inputlist = input()
    for i in inputlist.split(','):
        result = i
        subjectscore.append(result)
    #请在此添加代码,填入for循环遍历学生分数的代码
    #********** Begin *********#
    for  score in subjectscore:
    #********** End **********#
        score = int(score)
        sum = sum + score
    print("第%d位同学的总分为:%d" %(student,sum))

第4关 迭代器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
List = []
member = input()
for i in member.split(','):
    result = i
    List.append(result)
#请在此添加代码,将List转换为迭代器的代码
#********** Begin *********#
IterList=iter(List)
#********** End **********#
while True:
    try:
        #请在此添加代码,用next()函数遍历IterList的代码
        #********** Begin *********#
        num=next(IterList)
        #********** End **********#
        result = int(num) * 2
        print(result)
    except StopIteration:
        break

顺序与选择结构

第1关 顺序结构

1
2
3
4
5
6
7
8
9
10
changeOne = int(input())
changeTwo = int(input())
plus = int(input())

# 请在此添加代码,交换changeOne、changeTwo的值,然后计算changeOne、plus的和result的值
########## Begin ##########
changeOne,changeTwo=changeTwo,changeOne
result=changeOne+plus
########## End ##########
print(result)

第2关 选择结构:if-else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
workYear = int(input())
# 请在下面填入如果workYear < 5的判断语句
########## Begin ##########
if workYear<5:
########## End ##########
    print("工资涨幅为0")
# 请在下面填入如果workYear >= 5 and workYear < 10的判断语句
########## Begin ##########
elif workYear >= 5 and workYear < 10:
########## End ##########
    print("工资涨幅为5%")
# 请在下面填入如果workYear >= 10 and workYear < 15的判断语句
########## Begin ##########
elif workYear >= 10 and workYear < 15:
########## End ##########
    print("工资涨幅为10%")
# 请在下面填入当上述条件判断都为假时的判断语句
########## Begin ##########
else:
########## End ##########
    print("工资涨幅为15%")

第3关 选择结构 : 三元操作符

1
2
3
4
5
6
7
jimscore = int(input())
jerryscore = int(input())
# 请在此添加代码,判断若jim的得分jimscore更高,则赢家为jim,若jerry的得分jerryscore更高,则赢家为jerry,并输出赢家的名字
########## Begin ##########
winner = "jim" if jimscore > jerryscore else "jerry"
########## End ##########
print(winner)

Python分支结构训练

第1关 三位水仙花数

1
2
3
4
5
6
7
8
n=input()
num=0
for i in n:
    num+=int(i)**len(n)
if num==int(n):
    print("True")
else:
    print("False")

第2关 判断闰年

1
2
3
4
5
6
7
8
9
###begin
 #判断闰年
year=input()
year=int(year)
if year%4==0 and year%100!=0 or year%400==0:
    print("True")
else:
    print("False")
###end

第3关 考试成绩统计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
score=input()
score=score.split(",")
score=[int(i) for i in score]
result={"优秀":0,"良":0,"中":0,"及格":0,"不及格":0}
for i in score:
    if i>=90:
        result["优秀"]+=1
    elif i>=80:
        result["良"]+=1
    elif i>=70:
        result["中"]+=1
    elif i>=60:
        result["及格"]+=1
    else:
        result["不及格"]+=1
print(result)

第4关 简单的数字排序

1
2
3
4
5
6
7
8
9
10
###begin
a,b,c=map(int,input().split())
if a>b:
    a,b=b,a
if a>c:
    a,c=c,a
if b>c:
    b,c=c,b
print(a,b,c)
###end

第5关 求一元二次方程的解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
###begin
a,b,c=map(int,input().split())
if a==0:
    print("不是二次方程")
else:
    d=b*b-4*a*c
    if d<0:
        x1=-b/(2*a)
        x2=(-d)**0.5/(2*a)
        print('虚根1:%.2f+%.2fj\n虚根2:%.2f-%.2fj'%(x1,x2,x1,x2))
    elif d==0:
        x=-b/(2*a)
        print("有两个相等的实根:%.2f"%x)
    else:
        x1=(-b+d**0.5)/(2*a)
        x2=(-b-d**0.5)/(2*a)
        print("实根1:%.2f\n实根2:%.2f"%(x1,x2))
###end

第6关 输入一个年份和月份,打印出该月份有多少天。

1
2
3
4
5
6
7
8
9
10
year,month=map(int,input().split())
if month in [1,3,5,7,8,10,12]:
    print(31)
elif month in [4,6,9,11]:
    print(30)
else:
    if year%4==0 and year%100!=0 or year%400==0:
        print(29)
    else:
        print(28)

字符串处理

第1关 字符串的拼接:名字的组成

1
2
3
4
5
6
7
8
9
10
11
12
# coding=utf-8

# 存放姓氏和名字的变量
first_name = input()
last_name = input()

# 请在下面添加字符串拼接的代码,完成相应功能
########## Begin ##########
full_name=f"{first_name} {last_name}"
print(full_name)

########## End ##########

第2关 字符转换

1
2
3
4
5
6
7
8
9
# coding=utf-8
# 获取待处理的源字符串
source_string = input()
# 请在下面添加字符串转换的代码
########## Begin ##########
title_string=source_string.title()
print(title_string.strip())
print(len(source_string.strip()))
########## End ##########

第3关 字符串查找与替换

1
2
3
4
5
6
7
8
9
10
11
# coding = utf-8
source_string = input()

# 请在下面添加代码
########## Begin ##########
print(source_string.find('day'))
new_string=source_string.replace('day','time')
print(new_string)
print(new_string.split(' '))

########## End ##########

Python字符串训练

第1关 字符串格式化

1
2
3
4
5
6
7
8
9
###begin
str=input()
print(len(str))
num=int(str)
print("%d %o %x"%(num,num,num))
print("{:.6f} {:.6e}".format(num,num))
print("The number {} in hex is: {}, the number {} in oct is {}".format(num,hex(num),num,oct(num)))

###end

第2关 字符串常用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
str1 = "apple,pear,peach,banana,pear"
lst1 = ["apple","pear", "peach", "banana", "pear"]
###begin
print(str1.find('pear'),str1.rfind('pear'),str1.find('p'),str1.count('p'))

print(str1.split(','))
print(str1.split(' '))

print(str1.partition(','))
print(str1.partition(' '))

print(':'.join(lst1))
print('?'.join(lst1))
###end

第3关 文本规范化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
text = ''' abcdef 姓名:张三
年龄:39
性别男
职业  学生
籍贯:  地球 '''

###begin
text = text.strip(' abcdef')
text = text.replace(' ', '')
text = text.replace(':', ':')
text = text.replace('姓名:', '姓名:')
text = text.replace('年龄:', '年龄:')
text = text.replace('性别', '性别:')
text = text.replace('职业', '职业:')
text = text.replace('籍贯:', '籍贯:')
print(text)

###end

第4关 密码强度判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import string
password = input()
digits = string.digits
lowercase = string.ascii_lowercase
uppercase = string.ascii_uppercase
special = "',.!;?<>'"
flag_digits = False
flag_lowercase = False
flag_uppercase = False
flag_special = False
for i in password:
    if i in digits:
        flag_digits = True
    if i in lowercase:
        flag_lowercase = True
    if i in uppercase:
        flag_uppercase = True
    if i in special:
        flag_special = True

if len(password) < 6:
    print("密码过于简单。")
else:
    if (flag_digits + flag_lowercase + flag_uppercase + flag_special) == 1:
        print("密码强度弱。")
    elif (flag_digits + flag_lowercase + flag_uppercase + flag_special) == 2 or (flag_digits + flag_lowercase + flag_uppercase + flag_special) == 3:
        print("密码强度中。")
    else:
        print("密码强度强。")

第5关 句子倒序

1
2
3
4
5
6
7
8
9
10
11
12
13
###begin
def reverse_sentence(sentence):
    sentence = sentence.strip()
    if sentence[-1] in ['.', '!', '?']:
        symbol=sentence[-1]
        sentence = sentence[:-1]
    words = sentence.split()
    words.reverse()
    return ' '.join(words)+symbol
sentence = input()
print(reverse_sentence(sentence))

###end