鱼C论坛

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

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

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

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

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

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

  2. #准备工作
  3. import hashlib
  4. content = {}

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

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

  16. #MD5加密(encrypt())
  17. def encrypt():
  18.     bstr = bytes(password, "utf-8")
  19.     password = hashlib.md5(bstr)
  20.     content[name] = password
  21.    
  22.    
  23. #登陆(login())
  24. def login():
  25.     while True:
  26.         name = input("请输入用户名:")
  27.         if name not in content:
  28.             print("该用户名不存在。")
  29.         else:
  30.             break
  31.     while True:
  32.         password = input("请输入密码:")
  33.         if content[name] != hashlib.md5(password.encode()):
  34.             print("密码错误!")
  35.         else:
  36.             print("恭喜,登录成功~")
  37.             break
  38.     print("====================")   
  39.         
  40. #开始运行   
  41. print("欢迎来到鱼C论坛~")
  42. print("====================")
  43. print("""1. 注册
  44. 2. 登陆
  45. 3. 退出""")
  46. while True:
  47.     get_int()
  48.     if commond  == 1:
  49.         register()
  50.         encrypt()
  51.     elif commond == 2:
  52.         login()
  53.     elif commond == 3:
  54.         print("====================")
  55.         break
复制代码
运行的时候系统报错——commond没有定义,这个改如何解决呢?

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

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

  2. #准备工作
  3. import hashlib
  4. content = {}

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

  11. #注册(register())
  12. def register():
  13.     global name, password
  14.     name = input("请输入用户名:")
  15.     password = input("请输入密码:")
  16.     print("恭喜,注册成功~")
  17.     print("====================")

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

  24. #登陆(login())
  25. def login():
  26.     while True:
  27.         name = input("请输入用户名:")
  28.         if name not in content:
  29.             print("该用户名不存在。")
  30.         else:
  31.             break
  32.     while True:
  33.         password = input("请输入密码:")
  34.         if content[name] != hashlib.md5(password.encode()).hexdigest():
  35.             print("密码错误!")
  36.         else:
  37.             print("恭喜,登录成功~")
  38.             break
  39.     print("====================")   
  40.         
  41. #开始运行   
  42. print("欢迎来到鱼C论坛~")
  43. print("====================")
  44. print("""1. 注册
  45. 2. 登陆
  46. 3. 退出""")
  47. while True:
  48.     get_int()
  49.     if commond  == 1:
  50.         register()
  51.         encrypt()
  52.     elif commond == 2:
  53.         login()
  54.     elif commond == 3:
  55.         print("====================")
  56.         break
复制代码

(参考了https://fishc.com.cn/forum.php?m ... 464&pid=6322230)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

修改后的代码如下:

  1. #获取用户指令(get_int())
  2. def get_int():
  3.     commond = input("请输入指令:")
  4.     print("====================")
  5.     return int(commond)  # 将用户输入的指令转换为整数并返回

  6. #开始运行
  7. print("欢迎来到鱼C论坛~")
  8. print("====================")
  9. print("""1. 注册
  10. 2. 登陆
  11. 3. 退出""")
  12. while True:
  13.     commond = get_int()  # 调用get_int()函数获取用户指令
  14.     if commond == 1:
  15.         register()
  16.         encrypt()
  17.     elif commond == 2:
  18.         login()
  19.     elif commond == 3:
  20.         print("====================")
  21.         break
复制代码


这样修改后,用户输入的指令就会被正确地赋值给变量commond,从而避免了"commond没有定义"的错误。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

还有一点,command 需要是整数,可以在 10 和 11 行之间添加 commond = int(commond)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

  1. import hashlib

  2. content = {}

  3. def get_int():
  4.     commond = int(input("请输入指令:"))
  5.     print("====================")
  6.     return commond

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

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

  18. def login():
  19.     while True:
  20.         name = input("请输入用户名:")
  21.         if name not in content:
  22.             print("该用户名不存在。")
  23.         else:
  24.             break
  25.     while True:
  26.         password = input("请输入密码:")
  27.         encrypted_password = encrypt(password)
  28.         if content[name] != encrypted_password:
  29.             print("密码错误!")
  30.         else:
  31.             print("恭喜,登录成功~")
  32.             break
  33.     print("====================")

  34. print("欢迎来到鱼C论坛~")
  35. print("====================")
  36. print("""1. 注册
  37. 2. 登陆
  38. 3. 退出""")
  39. while True:
  40.     commond = get_int()
  41.     if commond == 1:
  42.         register()
  43.     elif commond == 2:
  44.         login()
  45.     elif commond == 3:
  46.         print("====================")
  47.         break
复制代码

主要修正了以下问题:

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

求最佳,有问题随时问
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

  2. #准备工作
  3. import hashlib
  4. content = {}

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

  11. #注册(register())
  12. def register():
  13.     name = input("请输入用户名:")
  14.     password_input = input("请输入密码:")
  15.     encrypted_password = encrypt(password_input)
  16.     content[name] = encrypted_password
  17.     print("恭喜,注册成功~")
  18.     print("====================")
  19.     return name
  20.     return password

  21. #MD5加密(encrypt())
  22. def encrypt(password):
  23.     encrypted_password = hashlib.md5(password.encode())
  24.     return encrypted_password
  25.       
  26. #登陆(login())
  27. def login():
  28.     while True:
  29.         name = input("请输入用户名:")
  30.         if name not in content:
  31.             print("该用户名不存在。")
  32.         else:
  33.             break
  34.     while True:
  35.         password = input("请输入密码:")
  36.         encrypted_password = encrypt(password)
  37.         if content[name] != encrypted_password:
  38.             print("密码错误!")
  39.         else:
  40.             print("恭喜,登录成功~")
  41.             break
  42.     print("====================")   
  43.         
  44. #开始运行   
  45. print("欢迎来到鱼C论坛~")
  46. print("====================")
  47. print("""1. 注册
  48. 2. 登陆
  49. 3. 退出""")
  50. while True:
  51.     command = get_int()
  52.     if command  == 1:
  53.         register()
  54.         encrypt()
  55.     elif command == 2:
  56.         login()
  57.     elif command == 3:
  58.         break
复制代码
第58行又错了,但是不知道错在哪里?

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

好像把encrypt()去掉就可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

  2. #准备工作
  3. import hashlib
  4. content = {}

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

  11. #注册(register())
  12. def register():
  13.     name = input("请输入用户名:")
  14.     password_input = input("请输入密码:")
  15.     encrypted_password = encrypt(password_input)
  16.     content[name] = encrypted_password
  17.     print("恭喜,注册成功~")
  18.     print("====================")

  19. #MD5加密(encrypt())
  20. def encrypt(password):
  21.     encrypted_password = hashlib.md5(password.encode())
  22.     return encrypted_password
  23.       
  24. #登陆(login())
  25. def login():
  26.     while True:
  27.         name = input("请输入用户名:")
  28.         if name not in content:
  29.             print("该用户名不存在。")
  30.         else:
  31.             break
  32.     while True:
  33.         password_test = input("请输入密码:")
  34.         encrypted_password = encrypt(password_test)
  35.         if content[name] != encrypted_password:
  36.             print("密码错误!")
  37.         else:
  38.             print("恭喜,登录成功~")
  39.             break
  40.     print("====================")   
  41.         
  42. #开始运行   
  43. print("欢迎来到鱼C论坛~")
  44. print("====================")
  45. print("""1. 注册
  46. 2. 登陆
  47. 3. 退出""")
  48. while True:
  49.     command = get_int()
  50.     if command  == 1:
  51.         register()
  52.     elif command == 2:
  53.         login()
  54.     elif command == 3:
  55.         break
复制代码
求助,谢谢



小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

他们是使用 CHatGPT的,我是人工,给我最佳
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

大哥可以帮我把问题解决了我就给你最佳
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

  2. #准备工作
  3. import hashlib
  4. content = {}

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

  11. #注册(register())
  12. def register():
  13.     global name, password
  14.     name = input("请输入用户名:")
  15.     password = input("请输入密码:")
  16.     print("恭喜,注册成功~")
  17.     print("====================")

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

  24. #登陆(login())
  25. def login():
  26.     while True:
  27.         name = input("请输入用户名:")
  28.         if name not in content:
  29.             print("该用户名不存在。")
  30.         else:
  31.             break
  32.     while True:
  33.         password = input("请输入密码:")
  34.         if content[name] != hashlib.md5(password.encode()).hexdigest():
  35.             print("密码错误!")
  36.         else:
  37.             print("恭喜,登录成功~")
  38.             break
  39.     print("====================")   
  40.         
  41. #开始运行   
  42. print("欢迎来到鱼C论坛~")
  43. print("====================")
  44. print("""1. 注册
  45. 2. 登陆
  46. 3. 退出""")
  47. while True:
  48.     get_int()
  49.     if commond  == 1:
  50.         register()
  51.         encrypt()
  52.     elif commond == 2:
  53.         login()
  54.     elif commond == 3:
  55.         print("====================")
  56.         break
复制代码

(参考了https://fishc.com.cn/forum.php?m ... 464&pid=6322230)
小甲鱼最新课程 -> https://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)

感谢大哥
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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



没有进行字符串转换操作

  1. #MD5加密(encrypt())
  2. def encrypt(password):
  3.     encrypted_password = hashlib.md5(password.encode())
  4.     return encrypted_password.hexdigest()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

好,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 03:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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