我的Python代码没有输出任何东西?
我目前正试图通过Eric Matthes编写的Python速成教程自学Python,我似乎在练习5-9时遇到了一些困难,关于使用if测试测试空列表 问题是:我的Python代码没有输出任何东西?,python,Python,我目前正试图通过Eric Matthes编写的Python速成教程自学Python,我似乎在练习5-9时遇到了一些困难,关于使用if测试测试空列表 问题是: usernames = ['admin', 'user_1', 'user_2', 'user_3', 'user_4'] for username in usernames: if username is 'admin': print("Hello admin, would you like to see a
usernames = ['admin', 'user_1', 'user_2', 'user_3', 'user_4']
for username in usernames:
if username is 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + username + ", thank you for logging in again.")
usernames = []
for username in usernames:
if username is 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + username + ", thank you for logging in again.")
if usernames:
print("Hello " + username + ", thank you for logging in again.")
else:
print("We need to find some users!")
5-9。无用户:将if测试添加到hello_admin.py以确保用户列表不为空
•如果列表为空,请打印我们需要查找某些用户的消息
•从列表中删除所有用户名,并确保打印正确的消息
这是我从hello\u admin.py获得的代码:
usernames = ['admin', 'user_1', 'user_2', 'user_3', 'user_4']
for username in usernames:
if username is 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + username + ", thank you for logging in again.")
usernames = []
for username in usernames:
if username is 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + username + ", thank you for logging in again.")
if usernames:
print("Hello " + username + ", thank you for logging in again.")
else:
print("We need to find some users!")
下面是我的5-9代码,它没有输出任何内容:
usernames = ['admin', 'user_1', 'user_2', 'user_3', 'user_4']
for username in usernames:
if username is 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + username + ", thank you for logging in again.")
usernames = []
for username in usernames:
if username is 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + username + ", thank you for logging in again.")
if usernames:
print("Hello " + username + ", thank you for logging in again.")
else:
print("We need to find some users!")
有没有人对我的代码没有输出的原因有任何反馈:“我们需要找到一些用户!”谢谢你的时间。:) 它不会输出任何内容,因为您的
if
和else
块位于迭代用户名的for
循环中。由于usernames
是一个空列表,它不会迭代任何内容,因此不会到达任何条件块
您可能希望改为:
usernames = []
for username in usernames:
if username is 'admin':
print("Hello admin, would you like to see a status report?")
else:
print("Hello " + username + ", thank you for logging in again.")
if usernames:
print("Hello " + username + ", thank you for logging in again.")
else:
print("We need to find some users!")
这将两次打印用户名列表中的最后一个用户名。第一个if-else应该在for循环中。第二个if-else块应该在外面
usernames = []
for username in usernames:
if username is 'admin':
print("Hey admin")
else:
print("Hello " + username + ", thanks!")
if usernames:
print("Hello " + username + ", thanks again.")
else:
print("Find some users!")
欢迎来到StackOverflow。将来,您可以通过缩进四个空格来格式化代码。看。否则,在第一个问题上做得很好。@FrankTan,我经常发现在这里格式化代码时手动为每行添加缩进是一件麻烦的事。有没有一种简单的方法可以在不复制和粘贴四个空格的情况下快速添加四个空格?除非您的代码在粘贴到此处之前尚未缩进,否则当您将代码放在花括号之间时,stackoverflow不会根据您的代码样式自动缩进吗?@ChuckLoganLim是的,有。通过缩进四个空格开始代码块。键入第一行。对于每个连续的行,请使用Ctrl+Enter
,而不仅仅是Enter
。这将自动为您缩进。这方面还有更多的内容。如果var是“string”
是一个坏习惯。它只适用于非常短的字符串。您应该改为使用=
进行比较。谢谢大家的回答,结果是我的for循环中的第二条if-else语句是问题所在,现在显示了我此练习的输出。:)