我正在做一个编程项目,我必须创建一个常见问题解答,允许用户添加新条目以及删除现有条目。到目前为止,所有功能都正常工作,但我遇到了问题。用户选择添加新条目后,条目添加成功。但是,如果他们选择添加另一个条目的选项,程序就会循环,就好像它不会再次调用该函数一样。我已经通读了我的教科书资源并在网上进行了一些搜索,但找不到解决方案。我也有一个问题,那就是获取异常来打印我的声明,但这不是关键因素。我不是在寻找可以直接复制到代码中的直接答案,只是一个例子。任何帮助是极大的赞赏。代码如下:
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')
慕慕森
慕的地10843
GCT1015
相关分类