第一题
ls = "123456789"
ls1 = "23456789456123"
result = ls.find(ls1)
if result != -1:
print("是")
else:
print("不是")
先写两个字符串ls = "123456789" 、ls1 = "23456789456123" 然后设置成 ls 来找 ls1 ,并且假设 if 里等于 -1 的话就打印 "是" , 不是的话就打印 "不是" 然后就以此来判断字符串是否相等
第二题
ls = "123456789"
ls1 = "23456789456123"
for char in ls:
if char not in ls1:
print("不在ls1里")
break
else:
print("在ls1里")
break
建立两个字符串 ls = "123456789" 和 ls1 = "23456789456123" ,运用for来判断字符是否相同 for char in ls1 来判断,最后用 if 来确定以及判断
第三题
ls = "123456789141252363343abcdefg"
if ls.isdigit() and ls.isalpha():
print("字符串里只有数字或者字母")
else:
print("字符串里既有数字或者字母")
用 ls.isdigit() 或者 ls.isalpha()来判断在 ls 字符串里是否有数字和字母
第四题
import sys
menu = """
"""
def show_info(info: str):
print("#" * 20)
print(info.center(20, "#"))
print("#" * 20)
while True:
print(menu)
num = input("点击进入登录页面")
if num == "1":
account = input("输入用户名")
password = input("请输入密码")
confirm = input("请确认密码")
if password != confirm:
show_info("密码错误,请再尝试一次")
else:
with open("account.txt", "at") as f:
text = account + ":" + password + "\n"
f.write(text)
show_info("注册成功")
elif num == "2":
account = input("请输入账户")
password = input("请输入密码")
with open("account.txt", "rt") as file:
text = file.readline()
local_account = text.split(":")[0]
local_password = text.split(":")[1].replace("\n", "2")
if account == local_account and local_password == password:
show_info("登陆成功")
else:
show_info("登陆失败")
elif num == "3":
show_info('谢谢使用')
sys.exit()
else:
print()
show_info("请输入有效数字".center(50, "1"))