鱼C论坛

 找回密码
 立即注册
查看: 1040|回复: 4

[已解决]请各位大神帮我看一下为什么 if '*' in str:这段代码运行时无法被执行,谢谢!

[复制链接]
发表于 2020-4-26 23:42:56 | 显示全部楼层 |阅读模式

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

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

x
correct = 'fishc.com'
i = 3
print("Please input password:")
str = input()

if correct == str:
    print("Welcome to my system!")
   
else:
    i = i - 1
    while i != 0:
        
        str = input("Please input right password,you have %d chance:\n" % i )
        
        if '*' in str:
            print("Please don\'t input * in password!")
            continue
        
        if correct == str:
            print("welcome to my system!")
            break
        
        else:
            i = i - 1
            continue
        if '*' in str:
            print("Please don\'t input * in password!")
            continue
        
    print("You\'re no chance!")
    print("Goodbye!")
   
print("Please wait......")               
最佳答案
2020-4-27 00:22:32
本帖最后由 sunrise085 于 2020-4-27 00:24 编辑

你的程序在while循环中有个if…else…,在if和else中分别有break和continue进行跳转,所以第二个if '*' in str:根本不会执行。
第一个if '*' in str:是会执行的。
但是你的程序有个问题:若第一次输入的str有*的话,次数仍然会减一,因为你在第一次input之后没有判断是否有*就直接减一了。
此外,str是python的内置函数,也是一个类,最好不要用它做变量名
  1. correct = 'fishc.com'
  2. i = 3
  3. pro="Please input password:"

  4. while i > 0:
  5.     str1 = input(pro)
  6.     if correct == str1:
  7.         print("welcome to my system!")
  8.         print("Please wait......")  
  9.         break
  10.    
  11.     else:
  12.         if '*' not in str1:
  13.             i=i-1
  14.         else:
  15.             print("Please don't input * in password!")
  16.         pro="Please input right password,you have %d chance:" % i
  17.         continue   
  18. else:
  19.     print("You\'re no chance!")
  20.     print("Goodbye!")
复制代码

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

使用道具 举报

发表于 2020-4-27 00:03:54 | 显示全部楼层
第二个
  1. if '*' in str:
复制代码
没用吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-27 00:15:56 | 显示全部楼层
if '*' in str:这段代码可以执行,但第二个有点多余
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-27 00:22:32 | 显示全部楼层    本楼为最佳答案   
本帖最后由 sunrise085 于 2020-4-27 00:24 编辑

你的程序在while循环中有个if…else…,在if和else中分别有break和continue进行跳转,所以第二个if '*' in str:根本不会执行。
第一个if '*' in str:是会执行的。
但是你的程序有个问题:若第一次输入的str有*的话,次数仍然会减一,因为你在第一次input之后没有判断是否有*就直接减一了。
此外,str是python的内置函数,也是一个类,最好不要用它做变量名
  1. correct = 'fishc.com'
  2. i = 3
  3. pro="Please input password:"

  4. while i > 0:
  5.     str1 = input(pro)
  6.     if correct == str1:
  7.         print("welcome to my system!")
  8.         print("Please wait......")  
  9.         break
  10.    
  11.     else:
  12.         if '*' not in str1:
  13.             i=i-1
  14.         else:
  15.             print("Please don't input * in password!")
  16.         pro="Please input right password,you have %d chance:" % i
  17.         continue   
  18. else:
  19.     print("You\'re no chance!")
  20.     print("Goodbye!")
复制代码

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

使用道具 举报

发表于 2020-4-27 00:28:27 From FishC Mobile | 显示全部楼层
str是个内置函数,换个变量名试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 11:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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