无法根据输入和条件获得正确的输出吗?

huangapple go评论65阅读模式
英文:

Can’t get proper output based upon input and conditions?

问题

以下是您的代码的中文翻译部分:

authorized_users = ("Sheldon", "Mark", "Daniel")
user_input = input("请输入您的姓名:")

if user_input in authorized_users:
    print("访问已授权!")
    print(f"你好,{user_input}!")
else:
    print("访问被拒绝!")

第4行有语法错误,应该将 "is in" 更改为 "in"。

英文:

I can’t get this code to output correctly. As you can see I’m asking a user to input a name that appears in a tuple and if that name is in said tuple they will be granted access. Otherwise, if the name isn’t in the variable they will be denied.

authorized_users = ("Sheldon", "Mark", "Daniel")
user_input = input("Please enter your name: ")

if user_input is in authorized_users:
 print("Access Granted!")
 print(f"Hello {user_input}!")
else:
 print("Access Denied!")

I just get a syntax error on line 4. I have tried changing is and in to just == to get the result I want, however, that doesn’t work.

authorized_users = ("Sheldon", "Mark", "Daniel")
user_input = input("Please enter your name: ")

if user_input is in authorized_users:
 print("Access Granted!")
 print(f"Hello {user_input}!")
else:
 print("Access Denied!")

答案1

得分: 2

Just get rid of the is. The in is a perfectly suitable operator by itself in this situation.

authorized_users = ("Sheldon", "Mark", "Daniel")
user_input = input("Please enter your name: ")

if user_input in authorized_users:
    print("Access Granted!")
    print(f"Hello {user_input}!")
else:
    print("Access Denied!")

You may wish to store the authorized_users tuple as all lowercase names, and then lowercase the input to make the check case-insensitive.

英文:

Just get rid of the is. The in is a perfectly suitable operator by itself in this situation.

authorized_users = ("Sheldon", "Mark", "Daniel")
user_input = input("Please enter your name: ")

if user_input in authorized_users:
 print("Access Granted!")
 print(f"Hello {user_input}!")
else:
 print("Access Denied!")

You may wish to store the authorized_users tuple as all lowercase names, and then lowercase the input to make the check case-insensitive.

huangapple
  • 本文由 发表于 2023年5月21日 03:53:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定