目的:通过flag来实现跳出while循环
编译结果:无法跳出while循环
原因:input()输入的是字符串而不是False;同理,即便输入0,"等等,都被编译器认为是字符串而不是表示False
flag=True while flag: print(flag) singer_name_input=input("which singer do you like?\n") album_name_input=input("what album of him do you like\n") flag=input("if you want to break,"+ "just input False\n") print(flag) def make_album(singer_name,album_name,songs_number=''): """build a dictionary about albums""" if songs_number: collection={ 'singer_name':singer_name.title(), 'album_name':album_name.title(), } collection['songs_number']=songs_number else: collection={ 'singer_name':singer_name.title(), 'album_name':album_name.title(), } return collection album=make_album(singer_name_input,album_name_input) print(album)
解决方案:如下
flag=True while flag: singer_name_input=input("which singer do you like?\n") album_name_input=input("what album of him do you like\n") answer=input("if you want to break,"+ "just input False\n") if answer=="False": flag=False