为了账号安全,请及时绑定邮箱和手机立即绑定

在 while 循环中对字典进行多次编辑

在 while 循环中对字典进行多次编辑

炎炎设计 2023-04-25 17:13:15
我正在做一个编程项目,我必须创建一个常见问题解答,允许用户添加新条目以及删除现有条目。到目前为止,所有功能都正常工作,但我遇到了问题。用户选择添加新条目后,条目添加成功。但是,如果他们选择添加另一个条目的选项,程序就会循环,就好像它不会再次调用该函数一样。我已经通读了我的教科书资源并在网上进行了一些搜索,但找不到解决方案。我也有一个问题,那就是获取异常来打印我的声明,但这不是关键因素。我不是在寻找可以直接复制到代码中的直接答案,只是一个例子。任何帮助是极大的赞赏。代码如下:import pyinputplus as pyip# Defines variables used in programdone = False #loop for input on menuuserQuestion = ''userAnswer = ''# Creates the menu for user interacionmenu = '''===========================Frequently Asked Quesstions===========================1: Exit2: List FAQ's3: Add FAQ4: Delete FAQ'''################################################ Creates dictionary and sets default values for FAQ for functionalityfaq = {'North Korea': 'Is afraid of clowns','Climate change': 'It is a lie.','America': 'Is burning.'}################################################ Function that prints a list of the current FAQsdef 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 inputdef 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')
查看完整描述

3 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

问题是 userQuestion 没有重置为空字符串,因此userQuestion not in faq在您第一次调用 Add_faq() 后将为 false,因此程序将永远不会在第一次迭代后进入 while 循环。



查看完整回答
反对 回复 2023-04-25
?
慕的地10843

TA贡献1785条经验 获得超8个赞

这真是一个好项目!您已经正确地发现了导致问题的模块,这很好。在 add_faq() 中,您将错误的变量设置为全局变量,此处已修复:


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



查看完整回答
反对 回复 2023-04-25
?
GCT1015

TA贡献1827条经验 获得超4个赞

所以我觉得自己很愚蠢,但我在发布后大约 20 分钟发现了我的错误。感谢 Viggy 的回复。在阅读和编码 3 个多小时后,我有点精疲力尽,所有东西都模糊不清,我感到很沮丧,哈哈。



查看完整回答
反对 回复 2023-04-25
  • 3 回答
  • 0 关注
  • 103 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信