[英]E319: No "python3" provider found. How can I solve this problem on SSH environment
[英]How can I solve if-else problem in for loops python3?
list = [["alex","tiffany"],["richard","bella"],["bruno","jessica"]]
enter = input("enter name: ")
for i in list:
if enter == i[0]:
print(i[1])
else:
print("the name that you searched for is absent!")
break
Output 1:
enter name: alex
->tifanny
Output 2:
enter name: richard
->the name that you searched for is absent!
我想打印“bella”,但我的程序不這樣做。 我怎么解決這個問題?
break
是在錯誤的地方。list
與可以導入的list
類型沖突。要了解有關編寫 Python 程序的標准的更多信息,請閱讀PEP8 ; 您還可以了解像 pylint 這樣的Python linter 。
這是一個工作程序。 它仍然可以更好,但我想讓你可以識別程序:
names = [["alex","tiffany"],["richard","bella"],["bruno","jessica"]]
name = input("Enter name: ")
found = False
for item in names:
if name == item[0]:
print(item[1])
found = True
break
if not found:
print("The name that you searched for is absent!")
這是在行動:
$ python x.py
Enter name: richard
The name that you searched for is absent!
bella
The name that you searched for is absent!
$ python x.py
Enter name: richard
bella
$ python x.py
Enter name: x
The name that you searched for is absent!
對上述答案的改進是使用for
循環的else
子句。 這樣你就不需要found
變量和第二個if
:
names = [["alex","tiffany"],["richard","bella"],["bruno","jessica"]]
name = input("Enter name: ")
for item in names:
if name == item[0]:
print(item[1])
break
else:
# This code will be executed if the for loop runs till its end
# without 'break' being called.
print("The name that you searched for is absent!")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.