jeudi 9 avril 2020

Can you help me clean up these nested if statements?

I am a beginner programmer, taking a class for university right now and I am making a program that uses two parallel lists to create the Christmas song: The Twelve Days of Christmas. I was able to do it but I have these if statements that are just an eyesore to look at and I couldn't figure out another way to do it. Are there any ideas? I was thinking a while loop but I am not sure what I can do with that here>

days = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh','twelfth']
gifts = ['Twelve drummers drumming,', 'Eleven pipers piping,', 'Ten lords a-leaping,', 'Nine ladies dancing,', 'Eight maids a-milking,', 'Seven swans a-swimming,', 'Six geese a-laying,', 'Five golden rings,', 'Four calling birds,', 'Three French Hens,', 'Two turtle doves,', 'A partridge in a pear tree.']

numberofdays = 11
for each in days:
  print("\nOn the " + each + " day of Christmas,")
  print("My true love sent to me")
  print(gifts[numberofdays])

  numberofdays = numberofdays - 1
  if numberofdays < 10:
        if numberofdays < 9:
            print(gifts[numberofdays + 2])
            if numberofdays < 8:
                print(gifts[numberofdays + 3])
                if numberofdays < 7:
                    print(gifts[numberofdays + 4])
                    if numberofdays < 6:
                        print(gifts[numberofdays + 5])
                        if numberofdays < 5:
                            print(gifts[numberofdays + 6])
                            if numberofdays < 4:
                                print(gifts[numberofdays + 7])
                                if numberofdays < 3:
                                    print(gifts[numberofdays + 8])
                                    if numberofdays < 2:
                                        print(gifts[numberofdays + 9])
                                        if numberofdays < 1:
                                            print(gifts[numberofdays + 10])
                                            if numberofdays < 0:
                                                print(gifts[numberofdays + 11])
        print("And a partridge in a pear tree")

Aucun commentaire:

Enregistrer un commentaire