鱼C论坛

 找回密码
 立即注册
查看: 875|回复: 13

[已解决]这个稍微有点难,刚学函数,大佬求教一下代码的问题

[复制链接]
发表于 2023-8-15 12:55:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
"""【注册】和【登陆】的代码"""

#准备工作
import hashlib
content = {}

#定义四个函数分别用于获取用户指令(get_int())、注册(register())、登陆(login())、MD5加密(encrypt())
#获取用户指令(get_int())
def get_int():
    commond = input("请输入指令:")
    print("====================")

#注册(register())
def register():
    name = input("请输入用户名:")
    password = input("请输入密码:")
    print("恭喜,注册成功~")
    print("====================")

#MD5加密(encrypt())
def encrypt():
    bstr = bytes(password, "utf-8")
    password = hashlib.md5(bstr)
    content[name] = password
    
    
#登陆(login())
def login():
    while True:
        name = input("请输入用户名:")
        if name not in content:
            print("该用户名不存在。")
        else:
            break
    while True:
        password = input("请输入密码:")
        if content[name] != hashlib.md5(password.encode()):
            print("密码错误!")
        else:
            print("恭喜,登录成功~")
            break
    print("====================")    
        
#开始运行    
print("欢迎来到鱼C论坛~")
print("====================")
print("""1. 注册
2. 登陆
3. 退出""")
while True:
    get_int()
    if commond  == 1:
        register()
        encrypt()
    elif commond == 2:
        login()
    elif commond == 3:
        print("====================")
        break
运行的时候系统报错——commond没有定义,这个改如何解决呢?

最佳答案
2023-8-15 14:53:57
本帖最后由 歌者文明清理员 于 2023-8-15 14:57 编辑
小儿无赖 发表于 2023-8-15 14:49
大哥可以帮我把问题解决了我就给你最佳

"""【注册】和【登陆】的代码"""

#准备工作
import hashlib
content = {}

#定义四个函数分别用于获取用户指令(get_int())、注册(register())、登陆(login())、MD5加密(encrypt())
#获取用户指令(get_int())
def get_int():
    global commond
    commond = int(input("请输入指令:"))
    print("====================")

#注册(register())
def register():
    global name, password
    name = input("请输入用户名:")
    password = input("请输入密码:")
    print("恭喜,注册成功~")
    print("====================")

#MD5加密(encrypt())
def encrypt():
    global password
    bstr = bytes(password, "utf-8")
    password = hashlib.md5(bstr).hexdigest()
    content[name] = password

#登陆(login())
def login():
    while True:
        name = input("请输入用户名:")
        if name not in content:
            print("该用户名不存在。")
        else:
            break
    while True:
        password = input("请输入密码:")
        if content[name] != hashlib.md5(password.encode()).hexdigest():
            print("密码错误!")
        else:
            print("恭喜,登录成功~")
            break
    print("====================")    
        
#开始运行    
print("欢迎来到鱼C论坛~")
print("====================")
print("""1. 注册
2. 登陆
3. 退出""")
while True:
    get_int()
    if commond  == 1:
        register()
        encrypt()
    elif commond == 2:
        login()
    elif commond == 3:
        print("====================")
        break
(参考了https://fishc.com.cn/forum.php?m ... 464&pid=6322230)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-15 12:56:28 | 显示全部楼层
问题出在函数get_int()中,变量commond没有定义。要解决这个问题,可以在函数get_int()中添加一个return语句,将用户输入的指令返回给调用方。然后在while循环中,将调用get_int()的结果赋值给变量commond。

修改后的代码如下:
#获取用户指令(get_int())
def get_int():
    commond = input("请输入指令:")
    print("====================")
    return int(commond)  # 将用户输入的指令转换为整数并返回

#开始运行
print("欢迎来到鱼C论坛~")
print("====================")
print("""1. 注册
2. 登陆
3. 退出""")
while True:
    commond = get_int()  # 调用get_int()函数获取用户指令
    if commond == 1:
        register()
        encrypt()
    elif commond == 2:
        login()
    elif commond == 3:
        print("====================")
        break

这样修改后,用户输入的指令就会被正确地赋值给变量commond,从而避免了"commond没有定义"的错误。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-15 13:00:54 | 显示全部楼层
这是因为commond在get_int()函数里定义, 也就是说出了函数这个变量就丢了

可以在 9 和 10 行之间添加一个:global commond,设置为全局变量

另外 “指令” 的英文是 command 而不是 commond;

还有一点,command 需要是整数,可以在 10 和 11 行之间添加 commond = int(commond)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-15 13:52:17 | 显示全部楼层
您的代码中存在一些问题,我已经对其进行了修正。请参考以下修正后的代码:
import hashlib

content = {}

def get_int():
    commond = int(input("请输入指令:"))
    print("====================")
    return commond

def register():
    name = input("请输入用户名:")
    password = input("请输入密码:")
    encrypted_password = encrypt(password)
    content[name] = encrypted_password
    print("恭喜,注册成功~")
    print("====================")

def encrypt(password):
    bstr = bytes(password, "utf-8")
    encrypted_password = hashlib.md5(bstr).hexdigest()
    return encrypted_password

def login():
    while True:
        name = input("请输入用户名:")
        if name not in content:
            print("该用户名不存在。")
        else:
            break
    while True:
        password = input("请输入密码:")
        encrypted_password = encrypt(password)
        if content[name] != encrypted_password:
            print("密码错误!")
        else:
            print("恭喜,登录成功~")
            break
    print("====================")

print("欢迎来到鱼C论坛~")
print("====================")
print("""1. 注册
2. 登陆
3. 退出""")
while True:
    commond = get_int()
    if commond == 1:
        register()
    elif commond == 2:
        login()
    elif commond == 3:
        print("====================")
        break
主要修正了以下问题:

get_int() 函数中,将 input() 的返回值转换为整数,并在函数末尾返回 commond。
在主循环中,使用 commond = get_int() 以获取用户输入的指令。
将 encrypt() 函数修改为接受一个参数 password,并返回加密后的密码。
在 register() 函数中,使用 encrypted_password = encrypt(password) 对密码进行加密,并将加密后的密码存储在 content 字典中。
在 login() 函数中,使用 encrypted_password = encrypt(password) 对输入的密码进行加密,然后将其与存储在 content 字典中的加密密码进行比较。

求最佳,有问题随时问
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-15 14:21:37 | 显示全部楼层
"""【注册】和【登陆】的代码"""

#准备工作
import hashlib
content = {}

#定义四个函数分别用于获取用户指令(get_int())、注册(register())、登陆(login())、MD5加密(encrypt())
#获取用户指令(get_int())
def get_int():
    command = input("请输入指令:")
    print("====================")
    return int(command)

#注册(register())
def register():
    name = input("请输入用户名:")
    password_input = input("请输入密码:")
    encrypted_password = encrypt(password_input)
    content[name] = encrypted_password
    print("恭喜,注册成功~")
    print("====================")
    return name
    return password

#MD5加密(encrypt())
def encrypt(password):
    encrypted_password = hashlib.md5(password.encode())
    return encrypted_password
      
#登陆(login())
def login():
    while True:
        name = input("请输入用户名:")
        if name not in content:
            print("该用户名不存在。")
        else:
            break
    while True:
        password = input("请输入密码:")
        encrypted_password = encrypt(password)
        if content[name] != encrypted_password:
            print("密码错误!")
        else:
            print("恭喜,登录成功~")
            break
    print("====================")    
        
#开始运行    
print("欢迎来到鱼C论坛~")
print("====================")
print("""1. 注册
2. 登陆
3. 退出""")
while True:
    command = get_int()
    if command  == 1:
        register()
        encrypt()
    elif command == 2:
        login()
    elif command == 3:
        break
第58行又错了,但是不知道错在哪里?

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-15 14:23:04 | 显示全部楼层
小儿无赖 发表于 2023-8-15 14:21
第58行又错了,但是不知道错在哪里?

好像把encrypt()去掉就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-15 14:29:09 | 显示全部楼层
sfqxx 发表于 2023-8-15 13:52
您的代码中存在一些问题,我已经对其进行了修正。请参考以下修正后的代码:

我修改了一下,但是又遇到问题了
在注册之后,我输入原来的密码得到的是密码错误,这是为啥啊
"""【注册】和【登陆】的代码"""

#准备工作
import hashlib
content = {}

#定义四个函数分别用于获取用户指令(get_int())、注册(register())、登陆(login())、MD5加密(encrypt())
#获取用户指令(get_int())
def get_int():
    command = input("请输入指令:")
    print("====================")
    return int(command)

#注册(register())
def register():
    name = input("请输入用户名:")
    password_input = input("请输入密码:")
    encrypted_password = encrypt(password_input)
    content[name] = encrypted_password
    print("恭喜,注册成功~")
    print("====================")

#MD5加密(encrypt())
def encrypt(password):
    encrypted_password = hashlib.md5(password.encode())
    return encrypted_password
      
#登陆(login())
def login():
    while True:
        name = input("请输入用户名:")
        if name not in content:
            print("该用户名不存在。")
        else:
            break
    while True:
        password_test = input("请输入密码:")
        encrypted_password = encrypt(password_test)
        if content[name] != encrypted_password:
            print("密码错误!")
        else:
            print("恭喜,登录成功~")
            break
    print("====================")    
        
#开始运行    
print("欢迎来到鱼C论坛~")
print("====================")
print("""1. 注册
2. 登陆
3. 退出""")
while True:
    command = get_int()
    if command  == 1:
        register()
    elif command == 2:
        login()
    elif command == 3:
        break
求助,谢谢



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-15 14:37:09 | 显示全部楼层
歌者文明清理员 发表于 2023-8-15 13:00
这是因为commond在get_int()函数里定义, 也就是说出了函数这个变量就丢了

可以在 9 和 10 行之间添加一 ...

感谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-15 14:37:45 | 显示全部楼层

他们是使用 CHatGPT的,我是人工,给我最佳
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-15 14:49:42 | 显示全部楼层
歌者文明清理员 发表于 2023-8-15 14:37
他们是使用 CHatGPT的,我是人工,给我最佳

大哥可以帮我把问题解决了我就给你最佳
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-15 14:53:57 | 显示全部楼层    本楼为最佳答案   
本帖最后由 歌者文明清理员 于 2023-8-15 14:57 编辑
小儿无赖 发表于 2023-8-15 14:49
大哥可以帮我把问题解决了我就给你最佳

"""【注册】和【登陆】的代码"""

#准备工作
import hashlib
content = {}

#定义四个函数分别用于获取用户指令(get_int())、注册(register())、登陆(login())、MD5加密(encrypt())
#获取用户指令(get_int())
def get_int():
    global commond
    commond = int(input("请输入指令:"))
    print("====================")

#注册(register())
def register():
    global name, password
    name = input("请输入用户名:")
    password = input("请输入密码:")
    print("恭喜,注册成功~")
    print("====================")

#MD5加密(encrypt())
def encrypt():
    global password
    bstr = bytes(password, "utf-8")
    password = hashlib.md5(bstr).hexdigest()
    content[name] = password

#登陆(login())
def login():
    while True:
        name = input("请输入用户名:")
        if name not in content:
            print("该用户名不存在。")
        else:
            break
    while True:
        password = input("请输入密码:")
        if content[name] != hashlib.md5(password.encode()).hexdigest():
            print("密码错误!")
        else:
            print("恭喜,登录成功~")
            break
    print("====================")    
        
#开始运行    
print("欢迎来到鱼C论坛~")
print("====================")
print("""1. 注册
2. 登陆
3. 退出""")
while True:
    get_int()
    if commond  == 1:
        register()
        encrypt()
    elif commond == 2:
        login()
    elif commond == 3:
        print("====================")
        break
(参考了https://fishc.com.cn/forum.php?m ... 464&pid=6322230)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-15 14:55:16 | 显示全部楼层
歌者文明清理员 发表于 2023-8-15 14:53
(参考了https://fishc.com.cn/forum.php?mod=redirect&goto=findpost&ptid=232464&pid=6322230)

感谢大哥
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-15 14:58:06 | 显示全部楼层
小儿无赖 发表于 2023-8-15 14:49
大哥可以帮我把问题解决了我就给你最佳



没有进行字符串转换操作
#MD5加密(encrypt())
def encrypt(password):
    encrypted_password = hashlib.md5(password.encode())
    return encrypted_password.hexdigest()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-15 15:03:28 | 显示全部楼层
wp231957 发表于 2023-8-15 14:58
没有进行字符串转换操作

好,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 19:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表