jeudi 31 janvier 2019

If statements within a while loop with user input

This code was working earlier, but now I'm having issues with the while 0 <= int(relationship) > 3 block of code. The user should be able choose the type of relationship to the letter recipient, which will populate a salutation and valediction into a letter.

If I keep the if/elif statements within the while loop, the sal/val strings don't populate into the letter.

If I bring the if/elif statements flush with the while loop, the strings populate for the letter ONLY if the user types 1, 2, or 3 on the first time around. If the user types a number besides 1, 2, or 3, the program will continue but the sal/val strings will not populate.

Originally, I only had one set of if/elif statements, but I created two (which is redundant, and also not fixing the issue) sets of statements to see if that would solve the problem. I've messed around with indentation as well. I also tried creating functions for each type of relationship, but I kept getting name errors for salutation.

I can get the program to work if the user follows directions the first time around, or have it run if the user is prompted to type 1, 2, or 3 after mistyping on the first try. But I can't get the sal/val to populate in both cases. I feel like the issue must be related to indentation, but when I change the indentation levels, the problem is't solved.

# Function for an infinite nested dictionary to add content to a personalized letter 
def lkng_glss_lttr_bot():             

    lgl_num = 0 
    while command == 'compose': 

        store_lkng_glss_lttr = {}
        new_lgl = {} 
        new_lgl_num = len(store_lkng_glss_lttr) + 1 
        store_lkng_glss_lttr[new_lgl_num] = new_lgl

        address_acquaintence = ['Dear', 'Cordially,']
        address_friend = ['Dearest', 'With warmest regards,']
        address_beloved = ['My Darling', 'With all my love and affection,']
        salutation = ''
        valediction = ''

        recipient = input("\nWhat is the recipient’s name?\n") 
        new_lgl['recipient'] = recipient

        email = input("\nWhat is the recipient's email?\n")
        new_lgl['email'] = email


        print("\nWhat is your relationship to %s?" % recipient)
        relationship = int(input("""Type 1 for Acquaintence
    Type 2 for Friend
    Type 3 for Beloved\n"""))

        while 0 <= int(relationship) > 3:
            relationship = input("Please type 1, 2, or 3.\n")

            if relationship == 1:
                for salutation in address_acquaintence:
                    salutation = address_acquaintence[0]                
                for valediction in address_acquaintence:
                    valediction = address_acquaintence[1]

            elif relationship == 2:    
                for salutation in address_friend:
                    salutation = address_friend[0]        
                for valediction in address_friend:
                    valediction = address_friend[1]

            elif relationship == 3:
                for salutation in address_beloved:
                    salutation = address_beloved[0]      
                for valediction in address_beloved:
                    valediction = address_beloved[1]

        if relationship == 1:
            for salutation in address_acquaintence:
                salutation = address_acquaintence[0]                
            for valediction in address_acquaintence:
                valediction = address_acquaintence[1]

        elif relationship == 2:    
            for salutation in address_friend:
                salutation = address_friend[0]        
            for valediction in address_friend:
                valediction = address_friend[1]

        elif relationship == 3:
            for salutation in address_beloved:
                salutation = address_beloved[0]      
            for valediction in address_beloved:
                valediction = address_beloved[1]



        print("\nPlease inquire about %s's well being." % recipient)
        inquiry = input("You can ask about the weather, or what %s has been doing to pass the time in the interim between your last exchange:\n" % recipient)

        body = input("\nPlease write a few sentences to inform %s of the recent happenings in your life:\n" % recipient)

        final_sentiment = input("\nPlease close the letter by expressing the importance of your friendship and your desire to spend time with %s in the future:\n" % recipient ) 

        sender = input("\nWhat is your name?\n")

        postscript = input("\nPlease write a short postscript to %s:\n" % recipient) 


        # Concatenate greeting and recipient
        part_1 = salutation + " " + recipient + "," + "\n"

        # Concatenate inquiry and body
        part_2 = inquiry + " " + body + "\n"

        # Concatenate part_1, part_2, final_sentiment, closing, sender, & postscript in reverse order for a non-tradional letter format
        non_trdtnl_lttr = "P.S. " + postscript + "\n" + sender + "\n" + valediction + "\n" + final_sentiment + "\n" + part_2 + part_1

        # Using the non-traditional letter format, reverse the order of the entire letter using an extended slice to create a Looking Glass Letter & store in dictionary
        lkng_glss_lttr = non_trdtnl_lttr[::-1]
        new_lgl['lkng_glss_lttr'] = lkng_glss_lttr


        # Notify sender that their letter contents have been added to the Bot
        print(store_lkng_glss_lttr)
        print("\nYour letter to %s has been composed by the Looking Glass Letter Bot:\n" % recipient + lkng_glss_lttr)
        print("\nYour Looking Glass Letter to %s will be emailed immediately." % recipient)


        # Ask user to add another client 
        print("\nWould you like to compose another letter? Y/N")
        sender_response = input().lower().strip() 


        if sender_response == 'y' or sender_response == 'yes': 
            continue 

        else:
            print("\nWe hope you enjoyed using the Looking Glass Letter Bot.\nWe look forward to serving your Looking Glass Letter composition needs in the future.")
            break 

The output should be something like:

,boB raeD

!doog m'I ?uoy era woH

!!uoy ssim I

,yllaidroC

G

.em llaC .S.P

But the Dear and Cordially, are not populating if the user types something besides 1, 2, or 3 and has to be prompted a second time.

Aucun commentaire:

Enregistrer un commentaire