鱼C论坛

 找回密码
 立即注册
查看: 1638|回复: 11

[已解决]pthon009作业题:密码程序,只有三次机会输入错误。

[复制链接]
发表于 2019-5-12 17:45:36 | 显示全部楼层 |阅读模式
3鱼币
大佬们好,

新手写了一个很麻烦的代码,但是还是不死心想针对遇到的问题咨询一下,贡献全部鱼bi

a.问题是python第九课的作业题,如图:
0. 设计一个验证用户密码程序,用户只有三次机会输入错误,不过如果用户输入的内容中包含"*"则不计算在内。
 WechatIMG7080.jpeg

b.代码如下:


temp = input("give the password, please:")
guess = str(temp)
password = "xixixi"
i=1
flag = 0

while i<4:
    if guess == password:
        flag = 1
        break
    elif i == 1:
        result = "*" in input()
        if result == False:
            print("you have still 2 chances")
            i=i+1
        else:
            print("no *  please, you have still 3 chances")
    elif i == 2:
        result = "*" in input()
        if result == False :
            print("you have still 1 chances")
            i=i+1
        else:
            print("no *  please, you have still 2 chances")
    elif i == 3:
        result = "*" in input()
        if result == False:
            print("you have no more chance")
        else:
            print("no *  please, you have still 1 chances")
            
if flag == 1:
    print("right, loading")

c. 能完成要求功能,但第一次输入的密码会被自动无视,从第二次输入密码才开始完成功能要求,如下,当第一次输入123456时,系统没有任何反应。

give the password, please:123456
123456
you have still 2 chances
123456*
no *  please, you have still 2 chances
123456*
no *  please, you have still 2 chances
123456
you have still 1 chances
123456
you have no more chance

d. 刚开始学习python,自己都知道自己写的很冗杂,有无数好的实现方法,但只是想知道自己错在哪里,以后可以避免。
最后不喜勿喷,如果觉得很鄙视请直接绕行,最后谢谢大家的支持~~
最佳答案
2019-5-12 17:45:37
password = "xixixi"
i = 1
flag = 0

while i < 4:
    temp = input("give the password, please:")
    guess = str(temp)
    if guess == password:
        flag = 1
        break
    else:
        if "*" not in temp:
            print("Password error,You have still %d chances" % (3 - i))
            i += 1
        else:
            print("no *  please,,You have still %d chances" % (3 - i))


if flag == 1:
    print("right, loading")

最佳答案

查看完整内容

password = "xixixi" i = 1 flag = 0 while i < 4: temp = input("give the password, please:") guess = str(temp) if guess == password: flag = 1 break else: if "*" not in temp: print("Password error,You have still %d chances" % (3 - i)) i += 1 else: print("no * please,,You have still %d chances" % (3 - ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-12 17:45:37 | 显示全部楼层    本楼为最佳答案   
password = "xixixi"
i = 1
flag = 0

while i < 4:
    temp = input("give the password, please:")
    guess = str(temp)
    if guess == password:
        flag = 1
        break
    else:
        if "*" not in temp:
            print("Password error,You have still %d chances" % (3 - i))
            i += 1
        else:
            print("no *  please,,You have still %d chances" % (3 - i))


if flag == 1:
    print("right, loading")
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-12 20:43:39 | 显示全部楼层

  1. myPwd = 'xixixi'
  2. pwd = input('请输入密码:')

  3. i = 2 # 输入错误的次数,加上 上面 input('请输入密码:') 总共3次
  4. while i:
  5.     if '*' in pwd:
  6.         pwd = input('密码中不能含有"*"号!你还有 %d 次机会!请输入密码' % (i+1)) # 输入 * 不算,所以i+1
  7.     else:
  8.         if pwd != myPwd:            
  9.             pwd = input('密码输入错误!你还有 %d 次机会!请输入密码' % i )
  10.             i -= 1
  11.             flag = 0
  12.         else:
  13.             flag = 1
  14.             break
  15.    
  16. if flag == 1:
  17.     print('密码正确, 进入程序......')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-13 16:40:40 | 显示全部楼层
wj2 发表于 2019-5-12 18:15
password = "xixixi"
i = 1
flag = 0

谢谢,不过这句应该改为如下,因为如果第一次输入有*号,应该还有三次机会。虽然没说出来我错误的地方,但有网友帮忙解决了,因为第一个回答的所以选为最佳,最后再次谢谢回复。
else:
            print("no *  please,,You have still %d chances" % (4 - i))
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-13 16:42:44 | 显示全部楼层

感谢热心回复,从中学到了规范的写法,因为有稍早回复的同学,所以选楼上回复为最佳答案,再次感谢哈。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-13 16:44:22 | 显示全部楼层
经qq群网友指出,错误的地方在这里
  result = "*" in input()
应该把input() 改为 tamp, 否则就还需要多输入一次
希望可以帮助有相同问题的同学哦
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-13 19:06:17 | 显示全部楼层
zhiyao 发表于 2019-5-13 16:42
感谢热心回复,从中学到了规范的写法,因为有稍早回复的同学,所以选楼上回复为最佳答案,再次感谢哈。

虽然楼上先答,然后选最佳也看楼主自愿。
不过我还是要说,楼上并没有达到要求,虽然和题意差不多,而我的才是的真正达到了要求。
所以要说最佳,我的才是最佳吧。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-14 17:38:13 | 显示全部楼层
ba21 发表于 2019-5-13 19:06
虽然楼上先答,然后选最佳也看楼主自愿。
不过我还是要说,楼上并没有达到要求,虽然和题意差不多,而我 ...

谢谢回复。
可以指点一下楼上哪里没达到要求呢,我修改数字3之后感觉达到了要求,刚开始学习python不太懂望见谅。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-14 18:50:26 | 显示全部楼层
zhiyao 发表于 2019-5-14 17:38
谢谢回复。
可以指点一下楼上哪里没达到要求呢,我修改数字3之后感觉达到了要求,刚开始学习python不太 ...

对着你发的图输一遍,不就一目了然了。这还要指点吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-14 21:24:33 | 显示全部楼层
ba21 发表于 2019-5-14 18:50
对着你发的图输一遍,不就一目了然了。这还要指点吗?

give the password, please:******
no *  please. You have still 3 chances
give the password, please:ILove***
no *  please. You have still 3 chances
give the password, please:***ILoveYou
no *  please. You have still 3 chances
give the password, please:小甲鱼是帅哥
Password error. You have still 2 chances
give the password, please:..鱼c好棒!!
Password error. You have still 1 chances
give the password, please:xixixi
right, loading

问题在哪里?
而且复制你的代码运行也有问题,最后也需要稍微改动才能运行,至少在我这里是这样,你自己也可以看一下。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-14 21:44:38 | 显示全部楼层
zhiyao 发表于 2019-5-14 21:24
give the password, please:******
no *  please. You have still 3 chances
give the password, pleas ...

没错,else改if就成了。他代码的问题相比之下还是没我的合题意吧,不会这也看不出来?
give the password, please:**
no *  please,,You have still 2 chances
give the password, please:asfd
Password error,You have still 2 chances
give the password, please:asdf
Password error,You have still 1 chances
give the password, please:asdf
Password error,You have still 0 chances

  1. myPwd = 'xixixi'
  2. pwd = input('请输入密码:')

  3. i = 2 # 输入错误的次数,加上 上面 input('请输入密码:') 总共3次
  4. while i:
  5.     if '*' in pwd:
  6.         pwd = input('密码中不能含有"*"号!你还有 %d 次机会!请输入密码' % (i+1)) # 输入 * 不算,所以i+1
  7.     else:
  8.         if pwd != myPwd:            
  9.             pwd = input('密码输入错误!你还有 %d 次机会!请输入密码' % i )
  10.             i -= 1
  11.             flag = 0
  12.             
  13.         if pwd == myPwd:
  14.             flag = 1
  15.             break
  16.    
  17. if flag == 1:
  18.     print('密码正确, 进入程序......')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-14 22:16:46 From FishC Mobile | 显示全部楼层
zhiyao 发表于 2019-5-14 21:24
give the password, please:******
no *  please. You have still 3 chances
give the password, pleas ...

谢谢回复,我上一层回复你说“我感觉修改数字3之后感觉达到了要求,不知道哪里还有问题”,就是把最后的3改成4就可以了,还把改完后按照图片输出了一遍复制回复你了。但你可能只是扫了一眼我的回复,没有看清我表达的内容。
但还是感谢问题回复,学到了更好的结构。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-25 03:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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