Python练习三,,1.使用while和
Python练习三,,1.使用while和
1.使用while和for循环分别打印字符串s=’asdfer’中每个元素。
s = "asdfer"index = 0while index < int(len(s)): a = s[index] print(a) index += 1
s = "asdfer"for i in s: print(i)
2.实现一个整数加法器,如用户输入5+9 ,5 + 9 ,5+ 9然后进行分割计算.
content = input("请输入式子:")index = content.find("+")a = int(content[0:index])b = int(content[index + 1:])print(a + b)
3.任意输入一串文字加数字,统计出数字的个数.
content = input ("请输入内容:")count = 0for i in content: if i.isdigit(): count += 1print("数字的个数为:{}" .format(count))
4.请用替换来把下列列表的林彬改为林兵。li = ["林风",["林彬","赖玉英", "赖狗屎"], "林静", "林圣翔", "林鹏"]
li = ["林风", ["林彬", "赖玉英", "赖狗屎"], "林静", "林圣翔", "林鹏"]li[1][0] = li[1][0].replace("彬", "兵")print(li)
5.将列表lis中的"tt"变成大写(用两种方式)。
lis = [2, 3, "k", ["qwe", 20,["k1", ["tt", 3, "1"]],89], "ab", "adv"]
# 方法一:lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]lis[3][2][1][0] = lis[3][2][1][0].upper()print(lis)# 方法二:lis = [2, 3, "k", ["qwe", 20, ["k1", ["tt", 3, "1"]], 89], "ab", "adv"]lis[3][2][1][0] = lis[3][2][1][0].replace("t", "T")print(lis)
Python练习三
评论关闭