【发布时间】:2016-02-05 23:25:46
【问题描述】:
在我的课堂上,我们被要求为音乐组织者创建一个包含三个选项的菜单,下面的代码是我整个代码的一部分。每当我运行程序时,我都不会出错,但是当我输入 1 时,终端只是再次弹出选项菜单,而不是“输入艺术家姓名:”
知道为什么吗?
# 创建选项菜单
option = 0
while option != 3:
print("What would you like to do?")
print(" 1. count all the songs by a particular artist")
print(" 2. print the contents of the database")
print(" 3. quit")
option = int(input("Please enter 1, 2, or 3: "))
# For option 1: find a all the songs on a certain album
if option == 1:
# Set the user input to a variable
Artistname = str("Enter artist name: ")
artistFound = False
for i in range(len(artistList)):
# For all the artist names in the list, compare the user input to #the artist names
if artistList[i] == Artistname:
artistFound = True
# count songs associate with artist
number+=1
print(count, "songs by",artistList[i])
# If the user input isn't in the list, then print out invalid
if artistFound == false:
print("Sorry, that is not an artist name")
【问题讨论】:
-
请关注PEP-8
-
尽可能避免使用索引。整个艺术家查找代码可以简化为
artistList.find(Artistname)或artistList.index(Artistname) -
count定义/分配在哪里?
标签: python loops count increment