赞
踩
字符串的拼接:名字的组成
# coding=utf-8
# 存放姓氏和名字的变量
first_name = input()
last_name = input()
# 请在下面添加字符串拼接的代码,完成相应功能
########## Begin ##########
full_name = first_name + " " +last_name
print (full_name)
########## End ##########
字符转换
# coding=utf-8
# 获取待处理的源字符串
source_string = input()
# 请在下面添加字符串转换的代码
########## Begin ##########
step1_source_string = source_string.strip()
step2_step1_source_string = step1_source_string.title()
print (step2_step1_source_string)
print(len(step2_step1_source_string))
########## End ##########
字符串查找与替换
# coding = utf-8
source_string = input()
# 请在下面添加代码
########## Begin ##########
print(source_string.find('day'))
step2_source_string = source_string.replace('day','time')
print(step2_source_string)
step3_source_string = step2_source_string.split(' ')
print(step3_source_string)
########## End ##########
行与缩进
#有错误的函数1 def wrong1(): print("wrong1") print("这里有一个错误缩进") #有错误的函数2 def wrong2(): print("wrong2") if False: print("这个不应该输出") print("这个也不应该输出") #有错误的函数3 def wrong3(): print("wrong3") print("hello world") #这里是调用三个函数的代码 #不要修改 if __name__ == '__main__': wrong1() wrong2() wrong3()
标识符与保留字
import keyword if __name__ == '__main__': #错误1 str1 = "string" print(str1) #错误2 woring1024 = 1024 print(woring1024) #错误3 float_1 = 1.024 print(float_1) #错误3 False_1 = False print(False_1) #在此处输出保留关键字 print(keyword.kwlist) print("end")
注释
if __name__ == '__main__': #以下是要修改的代码 print(1) #print(2) print(3) #print(4) print(5) #print(6) print("hello world") #print("这个不应该输出") """ print(1) print(2) """ print(3) print(4)
输入输出
if __name__ == "__main__":
a = int(input())
b = int(input())
# ********** Begin ********** #
print("%d + %d = %d" % (a,b,a+b))
print("%d - %d = %d" % (a,b,a-b))
print("%d * %d = %d" % (a,b,a*b))
print("%d / %d = %f" % (a,b,a/b))
# ********** End ********** #
列表元素的增删改:客人名单的变化
# coding=utf-8 # 创建并初始化Guests列表 guests = [] while True: try: guest = input() guests.append(guest) except: break # 请在此添加代码,对guests列表进行插入、删除等操作 ########## Begin ########## deleted_guest = guests.pop() guests.insert(2,deleted_guest) deleted_step3 = guests.pop(1) print(deleted_guest) print(guests) ########## End ##########
列表元素的排序:给客人排序
# coding=utf-8
# 创建并初始化`source_list`列表
source_list = []
while True:
try:
list_element = input()
source_list.append(list_element)
except:
break
# 请在此添加代码,对source_list列表进行排序等操作并打印输出排序后的列表
########## Begin ##########
source_list.sort()
print(source_list)
########## End ##########
用数字说话
# coding=utf-8
# 创建并读入range函数的相应参数
lower = int(input())
upper = int(input())
step = int(input())
# 请在此添加代码,实现编程要求
########## Begin ##########
data_list = list(range(lower,upper,step))
print(len(data_list))
print(max(data_list)-min(data_list))
########## End ##########
列表切片:你的菜单和我的菜单
# coding=utf-8 # 创建并初始化my_menu列表 my_menu = [] while True: try: food = input() my_menu.append(food) except: break # 请在此添加代码,对my_menu列表进行切片操作 ########## Begin ########## step1_menu = my_menu[::3] print(step1_menu) step2_menu = my_menu[-3:] print(step2_menu) ########## End ##########
顺序结构
changeOne = int(input())
changeTwo = int(input())
plus = int(input())
# 请在此添加代码,交换changeOne、changeTwo的值,然后计算changeOne、plus的和result的值
########## Begin ##########
temp=changeOne
changeOne=changeTwo
changeTwo=temp
result=changeOne+plus
########## End ##########
print(result)
选择结构:if-else
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%")
选择结构 : 三元操作符
jimscore = int(input())
jerryscore = int(input())
# 请在此添加代码,判断若jim的得分jimscore更高,则赢家为jim,若jerry的得分jerryscore更高,则赢家为jerry,并输出赢家的名字
########## Begin ##########
winner=('jim' if jimscore>jerryscore else 'jerry')
########## End ##########
print(winner)
函数的参数 - 搭建函数房子的砖
# coding=utf-8 # 创建一个空列表numbers numbers = [] # str用来存储输入的数字字符串,lst1是将输入的字符串用空格分割,存储为列表 str = input() lst1 = str.split(' ') # 将输入的数字字符串转换为整型并赋值给numbers列表 for i in range(len(lst1)): numbers.append(int(lst1.pop())) # 请在此添加代码,对输入的列表中的数值元素进行累加求和 ########## Begin ########## def s(*numbers): add=0 for i in numbers: add+=i return (add) d=s(*numbers) ########## End ########## print(d)
函数的返回值 - 可有可无的 return
# coding=utf-8
# 输入两个正整数a,b
a = int(input())
b = int(input())
# 请在此添加代码,求两个正整数的最大公约数
########## Begin ##########
def gcd(a,b):
if(b==0):
return a
else:
return(gcd(b,a%b))
########## End ##########
# 调用函数,并输出最大公约数
print(gcd(a,b))
函数的使用范围:Python 作用域
# coding=utf-8 # 输入两个正整数a,b a = int(input()) b = int(input()) # 请在此添加代码,求两个正整数的最小公倍数 ########## Begin ########## def gcd(a,b): if(b==0): return a else: return(gcd(b,a%b)) def lcm(a,b): temp=gcd(a,b) return int(a*b/temp) ########## End ########## # 调用函数,并输出a,b的最小公倍数 print(lcm(a,b))
While 循环与 break 语句
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 **********#
** for 循环与 continue 语句**
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,"的试卷已阅")
循环嵌套
studentnum = int(input()) #请在此添加代码,填入for循环遍历学生人数的代码 #********** Begin *********# for student in range(0,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))
迭代器
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
元组的使用:这份菜单能修改吗?
# coding=utf-
# 创建并初始化menu_list列表
menu_list = []
while True:
try:
food = input()
menu_list.append(food)
except:
break
# 请在此添加代码,对menu_list进行元组转换以及元组计算等操作,并打印输出元组及元组最大的元素
###### Begin ######
step1_menu_list = tuple(menu_list)
print(step1_menu_list)
print(max(step1_menu_list))
####### End #######
字典的使用:这份菜单可以修改
# coding=utf-8 # 创建并初始化menu_dict字典 menu_dict = {} while True: try: food = input() price = int(input()) menu_dict[food]= price except: break # 请在此添加代码,实现对menu_dict的添加、查找、修改等操作,并打印输出相应的值 ########## Begin ########## menu_dict['lamb'] = 50 print(menu_dict['fish']) menu_dict['fish'] = 100 del menu_dict['noodles'] print(menu_dict) ########## End ##########
字典的遍历:菜名和价格的展示
# coding=utf-8 # 创建并初始化menu_dict字典 menu_dict = {} while True: try: food = input() price = int(input()) menu_dict[food]= price except: break # 请在此添加代码,实现对menu_dict的遍历操作并打印输出键与值 ########## Begin ########## for key in menu_dict.keys(): print(key) for value in menu_dict.values(): print(value) ######### End ##########
嵌套 - 菜单的信息量好大
# coding=utf-8 # 初始化menu1字典,输入两道菜的价格 menu1 = {} menu1['fish']=int(input()) menu1['pork']=int(input()) # menu_total列表现在只包含menu1字典 menu_total = [menu1] # 请在此添加代码,实现编程要求 ########## Begin ########## menu2 = {} menu2['fish'] = menu1['fish'] * 2 menu2['pork'] = menu1['pork'] * 2 menu_total.append(menu2) ########## End ########## # 输出menu_total列表 print(menu_total)
内置函数 - 让你偷懒的工具
# coding=utf-8 # 输入一个整数n n = int(input()) # 请在此添加代码,对输入的整数进行判断,如果是素数则输出为True,不是素数则输出为False ########## Begin ########## def prime(n): if n == 1: return False else: for i in range(2, n): if n % i == 0: return False return True ########## End ########## print(prime(n))
函数正确调用 - 得到想要的结果
# coding=utf-8
# 输入数字字符串,并转换为数值列表
a = input()
num1 = eval(a)
numbers = list(num1)
# 请在此添加代码,对数值列表numbers实现从小到大排序
########## Begin ##########
result = sorted(numbers)
print(result)
########## End ##########
函数与函数调用 - 分清主次
# coding=utf-8
from math import pi as PI
n = int(input())
# 请在此添加代码,实现圆的面积计算,并输出面积结果
########## Begin ##########
def area(n):
area = PI * pow(n, 2)
return round(area, 2)
print('%.2f' %area(n))
########## End ##########
算术、比较、赋值运算符
# 定义theOperation方法,包括apple和pear两个参数,分别表示苹果和梨子的数量 def theOperation(apple,pear): # 请在此处填入计算苹果个数加梨的个数的代码,并将结果存入sum_result变量 ########## Begin ########## sum_result=apple+pear ########## End ########## print(sum_result) # 请在此处填入苹果个数除以梨的个数的代码,并将结果存入div_result变量 ########## Begin ########## div_result=apple/pear ########## End ########## print(div_result) # 请在此处填入苹果个数的2次幂的代码,并将结果存入exp_result变量 ########## Begin ########## exp_result=apple**2 ########## End ########## print(exp_result) # 请在此处填入判断苹果个数是否与梨的个数相等的代码,并将结果存入isequal变量 ########## Begin ########## isequal=(apple==pear) ########## End ########## print(isequal) # 请在此处填入判断苹果个数是否大于等于梨的个数的代码,并将结果存入ismax变量 ########## Begin ########## ismax=(apple>=pear) ########## End ########## print(ismax) # 请在此处填入用赋值乘法运算符计算梨个数乘以2的代码,并将结果存入multi_result变量 ########## Begin ########## multi_result=pear*2 ########## End ########## print(multi_result)
逻辑运算符
# 定义逻辑运算处理函数theLogic,其中tom与Jerry分别代表两个输入参数
def theLogic(tom,jerry):
# 请在此处填入jerry的布尔“非”代码,并将结果存入到not_result这个变量
########## Begin ##########
not_result=not jerry
########## End ##########
print(not_result)
# 请在此处填入tom,jerry的逻辑与代码,并将结果存入到and_result这个变量
########## Begin ##########
and_result=tom and jerry
########## End ##########
print(and_result)
位运算符
# 定义位运算处理函数bit, 其中bitone和bittwo两个参数为需要进行位运算的变量,由测试程序读入。 def bit(bitone,bittwo): # 请在此处填入将bitone,bittwo按位与的代码,并将运算结果存入result变量 ########## Begin ########## result=bitone & bittwo ########## End ########## print(result) # 请在此处填入将bitone,bittwo按位或的代码,并将运算结果存入result变量 ########## Begin ########## result=bitone | bittwo ######### End ########## print(result) # 请在此处填入将bitone,bittwo按位异或的代码,并将运算结果存入result变量 ########## Begin ########## result=bitone ^ bittwo ########## End ########## print(result) # 请在此处填入将bitone按位取反的代码,并将运算结果存入result变量 ########## Begin ########## result=(~bitone) ########## End ########## print(result) # 请在此处填入将bittwo左移动两位的代码,并将运算结果存入result变量 ########## Begin ########## result=(bittwo<<2) ########## End ########## print(result) # 请在此处填入将bittwo右移动两位的代码,并将运算结果存入result变量 ########## Begin ########## result=(bittwo>>2) ########## End ########## print(result)
成员运算符
# 定义成员片段函数member,参数me为待判断的人名,member_list为成员名单 def member(me,member_list = []): # 请在if后面的括号中填入判断变量me是否存在于list中的语句 ########## Begin ########## if( me in member_list): print("我是篮球社成员") else: print("我不是篮球社成员") ########## End ########## # 请在if后面的括号中填入判断变量me是否存在于list中的语句 ########## Begin ########## if(me not in member_list ): print("我不是篮球社成员") else: print("我是篮球社成员") ########## End ##########
身份运算符
# 定义addressone和addresstwo两个变量,并为其赋值 addressone = 20 addresstwo = 20 addressthree = 12 # 在if后面的括号中填入判断变量addressone与变量addresstwo是否有相同的存储单元的语句 ########## Begin ########## if(addressone is addresstwo): print("变量addressone与变量addresstwo有相同的存储单元") else: print("变量addressone与变量addresstwo的存储单元不同") ########## End ########## # 在if后面的括号中填入判断变量addresstwo与变量addressthree是否没有相同的存储单元的语句 ########## Begin ########## if(addresstwo is not addressthree): print("变量addresstwo与变量addressthree的存储单元不同") else: print("变量addresstwo与变量addressthree有相同的存储单元") ########## End ##########
运算符的优先级
# 定义并实现优先级运算函数theProirity
def thePriority(var1,var2,var3,var4):
# 先将var1左移两位,然后计算var1与var2的和,最后后将这个值乘以var3,并将最终结果存入result变量
########## Begin ##########
result=((var1<<2)+var2)*var3
########## End ##########
print(result)
# 先将var1与var2按位与,然后计算得到的值与var3的和,最后后将这个值乘以var4,并将最终结果存入result变量
########## Begin ##########
result=((var1 & var2)+var3)*var4
########## End ##########
print(result)
递归函数 - 汉诺塔的魅力
# coding=utf-8
# 输入正整数n
n = int(input())
# 请在此添加代码,对输入的正整数n进行阶乘运算,并输出计算结果。
########## Begin ##########
def fact(n):
if n == 1:
res = 1
else:
res = n * fact(n - 1)
return res
print (fact(n))
########## End ##########
lambda 函数 - 匿名函数的使用
# coding=utf-8
# 请在此添加代码,使用lambda来创建匿名函数,能够判断输入的两个数值的大小
########## Begin ##########
MAXIMUM = lambda x, y: max(x, y)
MINIMUM = lambda x, y: min(x, y)
########## End ##########
# 输入两个正整数
a = int(input())
b = int(input())
# 输出较大的值和较小的值
print('较大的值是:%d' % MAXIMUM(a,b))
print('较小的值是:%d' % MINIMUM(a,b))
Map-Reduce - 映射与归约的思想
# coding=utf-8 # 输入一个正整数 x = int(input()) # 请在此添加代码,将输入的一个正整数分解质因数 ########## Begin ########## def factor(x): if x == 1: return [] else: for i in range(2, x + 1): n, d = divmod(x, i) if d == 0: return [i] + factor(n) # 采用递归的方式 result = factor(x) ########## End ########## # 输出结果,利用map()函数将结果按照规定字符串格式输出 print(x,'=','*'.join(map(str,result)))
模块的定义
# coding=utf-8
import math
# 输入正整数a和b
a = float(input())
b = float(input())
# 请在此添加代码,输入直角三角形的两个直角边的边长a和b,计算出其斜边边长
########## Begin ##########
c = math.sqrt(pow(a, 2) + pow(b, 2))
print("%.3f" % c)
########## End ##########
内置模块中的内置函数
# coding=utf-8 # 导入math模块 import math # 输入两个整数a和b a = int(input()) b = int(input()) # 请在此添加代码,要求判断是否存在两个整数,它们的和为a,积为b ########## Begin ########## def isExist(x, y): c1 = x + math.sqrt(pow(x, 2) - 4 * y) > 0 and (x + math.sqrt(pow(x, 2) - 4 * y)) % 2 == 0 c2 = x - math.sqrt(pow(x, 2) - 4 * y) > 0 and (x - math.sqrt(pow(x, 2) - 4 * y)) % 2 == 0 if c1 and c2: return "Yes" else: return "No" print(isExist(a, b)) ########## End ##########
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。