Reputation: 41
I am making a text adventure game as part of my Learning Python book... Anyways this is what I am trying to do:
def is_a_song(song):
valid_sung_songs = ["song 1", "song 2", "song 3"]
valid_unsung_songs = ["s1", "s2", "s3"]
if song.lower() in valid_sung_songs:
return (True, 0)
elif song.lower() in valid_unsung_songs:
return (True, 1)
else:
return False
def favorite_song():
choice = raw_input("> ")
is_song, has_sung = is_a_song(choice)
if is_song and (has_sung == 0):
print "Good Song"
elif is_song and (has_sung == 1):
print "Good Song, But I haven't sung it"
else:
print "Not a valid song"
favorite_song()
Now this is just a cut down version of the code I actually used but when this is run this, it works when the song is valid and sung, when it is valid and unsung, but it crashes on the last else statement:
else:
print "Not a valid song"
With an error of:
TypeError: 'bool' object is not iterable
If you want the actual code I'm using it is here:
Upvotes: 4
Views: 11076
Reputation: 85512
You need to return False, False
here:
def is_a_song(song):
...
else:
return False, False
You call this function and try to unpack two values:
I would recommend to change your code to:
if song.lower() in valid_sung_songs:
return True, False
elif song.lower() in valid_unsung_songs:
return True, True
else:
return False, False
and later do this:
if is_song and not has_sung:
print "Good Song"
elif is_song and has_sung:
print "Good Song, But I haven't sung it"
else:
print "Not a valid song"
Seems to be cleaner to me.
is_song, has_sung = is_a_song(choice)
Upvotes: 4