Multiple edits to dictionary in while loop

Viewed 45

I'm working on a programming project where I have to create an FAQ that allows the user to add new entries as well as delete existing entries. So far all the functions work correctly, but I am running into an issue. After the user selects to add a new entry, the entry is added successfully. However, if they choose the option to add another entry, the program just loops as if it doesn't call the function again. I've read through my textbook sources and did some searching online but could not find a solution. I also have an issue getting the exception to print my statement, but that is not a critical factor. I'm not looking for a direct answer that I can just copy into the code, just an example to go off of. Any help is greatly appreciated. Code follows below:

import pyinputplus as pyip

# Defines variables used in program
done = False #loop for input on menu
userQuestion = ''
userAnswer = ''

# Creates the menu for user interacion
menu = '''
===========================
Frequently Asked Quesstions
===========================

1: Exit
2: List FAQ's
3: Add FAQ
4: Delete FAQ
'''
###############################################

# Creates dictionary and sets default values for FAQ for functionality
faq = {
'North Korea': 'Is afraid of clowns',
'Climate change': 'It is a lie.',
'America': 'Is burning.'
}
###############################################

# Function that prints a list of the current FAQs
def display_Faq():
    print('\nFrequently Asked Questions\n==========================')
    for question in faq:
        print('Question: ', question, '\nAnswer: ', faq[question], '\n')
    print() 
###############################################
    
# Function that adds to the FAQ based on user input
def Add_Faq():
    global userQuestion
    global userAnswer
    while userQuestion not in faq:
        try:
            userQuestion = input('\nPlease enter a question for the FAQs: ')
            userAnswer = input('\nPlease enter the answer: ')
            faq[userQuestion] = userAnswer
            print('\nEntry has been added to the FAQs.')
            break
        except:
            print(str(userQuestion) + ' already exists in FAQs, please rephrase.\n')
###############################################

# Function that checks user input against FAQ and deletes entries
def Del_Faq():
    global userQuestion
    userQuestion = input('\nEnter an entry to delete: ')
    if userQuestion in faq:
        del faq[userQuestion]
        print(str(userQuestion) + ' has been deleted from the FAQs')
    else:
        print(str(userQustion) + ' not exist in the FAQs, no changes have been made.')
###############################################
    
# Actual program that runs based off user input
while not done:
    print(menu)
    try:
        selection = pyip.inputInt(prompt = '\nPlease enter menu item 1-4: ', min=1, max=4)
        if selection == 1:
            done = True
        elif selection == 2:
            display_Faq()
        elif selection == 3:
            Add_Faq()
        elif selection == 4:
            Del_Faq()
    except pyip.PyInputPlusException:
        print('Please check your input and try again')
        continue
3 Answers

The problem is that userQuestion is not reset to empty string so userQuestion not in faq will be false after the first time you call Add_faq() hence the program will never enter the while loop after first iteration.

This is a really good project! You've correctly spotted the module causing you problem, which is good. In add_faq(), you're setting the wrong variables as globals, here it is fixed:

import pyinputplus as pyip

# Defines variables used in program
done = False #loop for input on menu
userQuestion = ''
userAnswer = ''

# Creates the menu for user interacion
menu = '''
===========================
Frequently Asked Quesstions
===========================

1: Exit
2: List FAQ's
3: Add FAQ
4: Delete FAQ
'''
###############################################

# Creates dictionary and sets default values for FAQ for functionality
faq = {
'North Korea': 'Is afraid of clowns',
'Climate change': 'It is a lie.',
'America': 'Is burning.'
}
###############################################

# Function that prints a list of the current FAQs
def display_Faq():
    print('\nFrequently Asked Questions\n==========================')
    for question in faq:
        print('Question: ', question, '\nAnswer: ', faq[question], '\n')
    print() 
###############################################
    
# Function that adds to the FAQ based on user input
def Add_Faq():
    global faq
    while True:
        userQuestion = input('\nPlease enter a question for the FAQs: ')
        userAnswer = input('\nPlease enter the answer: ')
        if userQuestion not in faq:
            faq[userQuestion] = userAnswer
            print('\nEntry has been added to the FAQs.')
            break
        else:
            print(str(userQuestion) + ' already exists in FAQs, please rephrase.\n')
###############################################

# Function that checks user input against FAQ and deletes entries
def Del_Faq():
    global faq
    userQuestion = input('\nEnter an entry to delete: ')
    if userQuestion in faq:
        del faq[userQuestion]
        print(str(userQuestion) + ' has been deleted from the FAQs')
    else:
        print(str(userQustion) + ' not exist in the FAQs, no changes have been made.')
###############################################
    
# Actual program that runs based off user input
while not done:
    print(menu)
    try:
        selection = pyip.inputInt(prompt = '\nPlease enter menu item 1-4: ', min=1, max=4)
        if selection == 1:
            done = True
        elif selection == 2:
            display_Faq()
        elif selection == 3:
            Add_Faq()
        elif selection == 4:
            Del_Faq()
    except pyip.PyInputPlusException:
        print('Please check your input and try again')
        continue

So I feel stupid but I found my error about 20 minutes after posting. I appreciate the response Viggy. I was just a bit burned out after reading and coding for 3+ hours everything was just blurred together and I was getting frustrated lol.

Related