|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
是这样的,其实不只今天,以前也有几次遇到这种问题:
- def make_album(singer, album, songs=None):
- a = {'singer' : singer, 'album' : album}
- if songs:
- a['songs'] = songs
- return a
- b = True
- while b:
- a_singer = input("Enter a singer name here. ")
- a_album = input("Enter one of his/her albums here. ")
- ques = input("Would you like to tell us how many songs are in that album?(y/n) ")
- if ques == 'y' or 'Y': # 问题出在这里,也许下面那个if也会有问题。
- a_songs = input("Enter the number of the songs here. ")
- else:
- askif = input("Would you like to fill this poll again?(y/n) ")
- if askif == 'n' or 'N':
- b = False
- e = make_album(a_singer, a_album, a_songs)
- print(e)
复制代码
代码中的注释部分,我没明白为什么我输入的明明不是y或Y,他仍然回执行他下面缩进级里的东西。
下面那边还有一个if,我怀疑那个也会出现相同的问题。
这是什么原因导致的呢?
应该如何设计条件比较好呢?
这样改:
非空字符串都可以看做True,所以即使ques不是y,整个if也为True,会执行if下面的代码。
|
|