重点作业

Posted monster7

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了重点作业相关的知识,希望对你有一定的参考价值。

---恢复内容开始---

#实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
s = 3
while s>0:
    name = input("请输入用户名:").strip()
    password = input("请输入密码:").strip()
    if name == "seven" or name == "alex" and password =="123":
        print("登录成功")
        break
    else:
        print("登录失败,你还有%s次机会" %s)
        s -= 1
#使用 while 循环实现输出 2 -3 + 4-5 + 6 ... + 100的和
sum = 0
a = 2
while a<=100:
    if a%2==0:
        sum += a
    else:
        sum -= a
    a += 1
print(sum)
#使用 for 循环和 range 实现输的1 -2 + 3-4 + 5-6 ... + 99的和
sum = 0
for i in range(100):
    if i % 2 == 0:
        sum -= i
    else:
        sum += i
print(sum)
#使用 while 循环实现输出  1,2,3,4,5,7,8,9,11,12
a = 1
while a<13:
    print(a)
    a += 1
#使用 while 循环实现输出 1-100 内的所有奇数
a = 1
while a <= 100:
    if a % 2 == 0:
        pass
    else:
        print(a)
    a += 1
#分别书写数字 5,10,32,7 的二进制表示
for i in [5,10,32,7]:
    print("{}的二进制数是{}".format(i,bin(i)))
    print("%s的二进制数是%s" %(i, bin(i)))
#输入一年份,判断该年份是否是闰年并输出结果。(编程题)
#注:凡符合下面两个条件之一的年份是闰年。 (1) 能被4整除但不能被100整除。 (2) 能被400整除。
year = input("请输入您需要查询的年份:").strip()
year = int(year)

if year % 4 == 0 and year % 100 != 0 or year % 400 ==0:
    print("%d是闰年" %year)
#编写登陆接口
# 基础需求:
# 让用户输入用户名密码
# 认证成功后显示欢迎信息
# 输错三次后退出程序

count = 3
while count > 0:
    name = input("帐号:")
    password = input("密码:")
    if name == "long" and password == "123":
        print("欢迎登录")
        break
    else:
        count -= 1
        print("登录失败,你还有%d次机会" %count)
#如有一个变量n1 = 5,请使用int提供的方法,得到该变量最少可以用多少个二进制位表示?
n1 = 5
print(int.bit_length(n1))
#判断name变量对应的值是否以"al"开头,并输出结果

name=" aleX"
print(name.startswith("al"))
#将name变量对应的值中的"l"替换为"p",并输出结果
name=" aleX"
print(name.replace("l","p"))
#请输出name变量对应的值的后2个字符?
name=" aleX"
print(name[-2:])  #-1代表最后一个字符,-2代表倒数第二个字符
#获取子序列,仅不包含最后一个字符。如:oldboy 则获取oldbo;root则获取roo
name=" aleX"
print(name.rstrip(name[-1:]))
#利用下划线将列表的每一个元素拼接成字符串,li = "alexericrain"
li = "alexericrain"
print("_".join(li))
#计算用户输入的内容中有几个十进制数?几个字母?
#如:
#content=input(‘请输入内容:‘) #如: asduiaf878123jkjsfd-213928
content = input(请输入内容:) .strip()  #一定要记得使用strip去除空格
v,r = content.split("+")
print(int(v)+int(r))
#计算用户输入的内容中有几个十进制数?几个字母?
# 如:
# content=input(‘请输入内容:‘) #如: asduiaf878123jkjsfd-213928
content=input(请输入内容:).strip()
v = 0
s = 0
for i in content:
    if i.isdecimal():
        v += 1
    if i.isalpha():
        s += 1
    else:
        pass
print("数字的个数是%d,字母的个数是%d" %(v,s))
while True:
    name = input("请输入用户名:")
    if name == "q" or name == "Q":
        break
    if len(name) <= 20:
        pass
    else:
        name = name[:20]
    password = input("请输入密码:")
    if password == "q" or password == "Q":
        break
    if len(password) <= 20:
        pass
    else:
        password = password[:20]
    email = input("请输入邮箱:")
    if email == "q" or email == "Q":
        break
    if len(email) <= 20:
        pass
    else:
        email = email[:20]
    template = "{0}\t{1}\t{2}\n"
    v = template.format(name,password,email)
    break
print(v.expandtabs(20))  #将\t以及\n转化为空格或者回车
#往names列表里black_girl前面插入一个alex
names = ["old_driver","rain","jack","shanshan","peiqi","black_girl"]
names.insert(names.index("black_girl"),"alex") #占有原来的位置
print(names)
#把shanshan的名字改成中文,姗姗
names = ["old_driver","rain","jack","shanshan","peiqi","black_girl"]
names[names.index("shanshan")] = "姗姗"
print(names)
#往names列表里rain的后面插入一个子列表,["oldboy","oldgirl"]
names = ["old_driver","rain","jack","shanshan","peiqi","black_girl"]
names.insert(names.index("rain")+1,["oldboy","oldgirl"])
print(names)
#创建新列表[1,2,3,4,2,5,6,2],合并入names列表
names = ["old_driver","rain","jack","shanshan","peiqi","black_girl"]
names.extend([1,2,3,4,5,6,2])
print(names)
#.取出names列表中索引2-10的元素,步长为2
names = ["old_driver","rain","jack","shanshan","peiqi","black_girl"]
print(names[2:10:2])
#循环names列表,打印每个元素的索引值,和元素
names = ["old_driver","rain","jack","shanshan","peiqi","black_girl"]
for index,name in enumerate(names):
    print("%s,%s" %(index,name))
#循环names列表,打印每个元素的索引值,和元素,当索引值为偶数时,把对应的元素改为-1
names = ["old_driver","rain","jack","shanshan","peiqi","black_girl"]
for index,name in enumerate(names):
    if index % 2 == 0:
        name = "-1"
    else:
        pass
    print(index,name)
#这一题没理解
#
names里有3个2,请返回第2个2的索引值。不要人肉数,要动态找(提示,找到第一个2的位置,在次基础上再找第2个) print(names.index(2,names.index(2)+1))

 

#查找列表(或元祖或字典)中元素,移除每个元素的空格,并查找以 a 或 A 开头 并且以 c 结尾的所有元素
# li = ["alec", " aric", "Alex", "Tony", "rain"]
# tu = ("alec", " aric", "Alex", "Tony", "rain")
# dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘,"k3": "Alex", "k4": "Tony"}
li = ["alec", " aric", "Alex", "Tony", "rain"]
for i in li:
    i = i.strip()
    c_a = i.startswith("a")
    c_A = i.startswith("A")
    c_c = i.endswith("d")
    if c_a or c_A and c_c:
        print(i)

tu = ("alec", " aric", "Alex", "Tony", "rain")
for i in tu:
    i = i.strip()
    c_a = i.startswith("a")
    c_A = i.startswith("A")
    c_c = i.endswith("d")
    if c_a or c_A and c_c:
        print(i)

dic = {k1: "alex", k2:  aric,"k3": "Alex", "k4": "Tony"}
for i in dic:
    i = i.strip()
    c_a = i.startswith("a")
    c_A = i.startswith("A")
    c_c = i.endswith("d")
    if c_a or c_A and c_c:
        print(i)
#列表中追加元素 “seven”,并输出添加后的列表
li = [alex,eric,rain]
li.append("seven")
print(li)
#请在列表的第 1 个位置插入元素 “Tony”,并输出添加后的列表 
li = [alex,eric,rain]
li.insert(0,"Tony")  #append是追加 insert是在指定位置添加
print(li)

 

---恢复内容结束---

以上是关于重点作业的主要内容,如果未能解决你的问题,请参考以下文章

201621123062《java程序设计》第九周作业总结

第九次作业

车间作业分析重点分析的内容是哪些?

车间作业分析重点分析的内容是哪些?

第二周作业

重点作业