Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当我尝试从我创建的菜单中选择一个sepcific选项时,它只是再次输出菜单_Python_Loops_Count_Increment - Fatal编程技术网

Python 当我尝试从我创建的菜单中选择一个sepcific选项时,它只是再次输出菜单

Python 当我尝试从我创建的菜单中选择一个sepcific选项时,它只是再次输出菜单,python,loops,count,increment,Python,Loops,Count,Increment,在我的课堂上,我们被要求为音乐组织者创建一个包含三个选项的菜单,下面的代码是我全部代码的一部分。每当我运行程序时,不会出现错误,但当我输入1时,终端会再次弹出选项菜单,而不是“输入艺术家名称”: 知道为什么吗 #创建选项菜单 option = 0 while option != 3: print("What would you like to do?") print(" 1. count all the songs by a particular artist") p

在我的课堂上,我们被要求为音乐组织者创建一个包含三个选项的菜单,下面的代码是我全部代码的一部分。每当我运行程序时,不会出现错误,但当我输入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")

尝试此操作以获取用户输入

Artistname =input("Enter artist name: ")
使用“中断”退出while循环或按3键:

Artistname = str("Enter artist name: ")
将字符串
“输入艺术家名称:”
绑定到名为
Artistname
的变量,因此您的代码正在
artistList
中查找值为
“输入艺术家名称:”
——可能不在列表中的项目

另一个问题是:

if artistFound == false:
false
在Python中不是有效的标识符。这应该会引发异常,如果不是,您可能在其他地方为
false
分配了一个值

要解决此问题,您需要从用户处获取输入:

artist_name = input("Enter artist name: ")
现在
artist\u name
将包含用户输入的一些值,您的搜索代码现在将查找列表中更可能存在的内容

请注意,您的搜索代码可以通过以下方式大幅减少:


请尽量避免使用索引。整个艺术家查找代码可以简化为
artistList.find(Artistname)
artistList.index(Artistname)
定义/分配的
count
在哪里?如果您使用的是python2,即python3的输入()我使用的是python3.4,我应该对这个版本做些不同的操作吗?我试着将其更改为原始输入,结果显示为“输入艺术家名称:”,但当我尝试在其中键入艺术家时,它会再次输出菜单。我的错误…我的意思是,对于python3,它的输入()…请查看您要求用户输入选项的行,因为它找不到任何艺术家,并且您没有使用任何中断…它仍然处于while循环中
artist_name = input("Enter artist name: ")
artist_count = artist_list.count(artist_name)
if artist_count > 0:
    print('{} songs by {}'.format(artist_count, artist_name))
else:
    print("Sorry, that is not an artist name")