【问题标题】:The Code is not going as i expected , i wanted if there is wrong input it will say wrong , and if the input is right it will say Yes [duplicate]代码没有像我预期的那样运行,我想如果输入错误,它会说错,如果输入正确,它会说是 [重复]
【发布时间】:2020-08-17 18:40:11
【问题描述】:

密码输入正确会打印('OKAY'),输入错误会打印('wrong password')

name=input("Enter your name: ")
print("Hello, " + name)


LEGAL_AGE=18
 age = int(input("Please enter your age: "))

 if age<18 :
    print("GO HOME")
 else:

    password =input("TYPE YOUR PASSWORD : ")


    if password==int(12345):
       print('YES , YOU CAN ENTER NOW')
 else:
    print('WRONG')

【问题讨论】:

  • 您将12345 转换为int(它已经是int)而不是password

标签: python input passwords


【解决方案1】:

你需要这样做才能让它工作 -

name=input("Enter your name: ")
print("Hello, " + name)


LEGAL_AGE=18
age = int(input("Please enter your age: "))

if age<18 :
    print("GO HOME")
    
else:
    password =input("TYPE YOUR PASSWORD : ")
    
    if int(password)==12345:
       print('YES , YOU CAN ENTER NOW')
    else:
        print('WRONG')

在您的代码中,您正在将语句int(12345) 中的整数转换为int,这基本上没有任何效果。您需要将密码转换为整数,python 将其作为字符串读取。

请注意,您不需要将密码转换为整数,您可以对照字符串进行检查,例如 -

name=input("Enter your name: ")
print("Hello, " + name)


LEGAL_AGE=18
age = int(input("Please enter your age: "))

if age<18 :
    print("GO HOME")
    
else:
    password =input("TYPE YOUR PASSWORD : ")
    
    if password=='12345':
       print('YES , YOU CAN ENTER NOW')
    else:
        print('WRONG')

以上两种方法都是正确的。

【讨论】:

  • 非常感谢兄弟,你太棒了!!!!!!!
  • @MDZIAURRAHMAN 很高兴为您提供帮助。如果有帮助,您可以投票/标记为已接受
猜你喜欢
  • 1970-01-01
  • 2022-01-25
  • 2016-07-02
  • 1970-01-01
  • 1970-01-01
  • 2022-12-06
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多