马钰乔布轩 发表于 2020-4-26 23:42:56

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

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......")               

cug_cui 发表于 2020-4-27 00:03:54

第二个 if '*' in str:没用吧

ouyunfu 发表于 2020-4-27 00:15:56

if '*' in str:这段代码可以执行,但第二个有点多余

sunrise085 发表于 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的内置函数,也是一个类,最好不要用它做变量名
correct = 'fishc.com'
i = 3
pro="Please input password:"

while i > 0:
    str1 = input(pro)
    if correct == str1:
      print("welcome to my system!")
      print("Please wait......")
      break
   
    else:
      if '*' not in str1:
            i=i-1
      else:
            print("Please don't input * in password!")
      pro="Please input right password,you have %d chance:" % i
      continue   
else:
    print("You\'re no chance!")
    print("Goodbye!")

jkluoling1992 发表于 2020-4-27 00:28:27

str是个内置函数,换个变量名试试
页: [1]
查看完整版本: 请各位大神帮我看一下为什么 if '*' in str:这段代码运行时无法被执行,谢谢!