英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论