第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 while 循環(huán)中對字典進行多次編輯

在 while 循環(huán)中對字典進行多次編輯

炎炎設(shè)計 2023-04-25 17:13:15
我正在做一個編程項目,我必須創(chuàng)建一個常見問題解答,允許用戶添加新條目以及刪除現(xiàn)有條目。到目前為止,所有功能都正常工作,但我遇到了問題。用戶選擇添加新條目后,條目添加成功。但是,如果他們選擇添加另一個條目的選項,程序就會循環(huán),就好像它不會再次調(diào)用該函數(shù)一樣。我已經(jīng)通讀了我的教科書資源并在網(wǎng)上進行了一些搜索,但找不到解決方案。我也有一個問題,那就是獲取異常來打印我的聲明,但這不是關(guān)鍵因素。我不是在尋找可以直接復制到代碼中的直接答案,只是一個例子。任何幫助是極大的贊賞。代碼如下: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條經(jīng)驗 獲得超17個贊

問題是 userQuestion 沒有重置為空字符串,因此userQuestion not in faq在您第一次調(diào)用 Add_faq() 后將為 false,因此程序?qū)⒂肋h不會在第一次迭代后進入 while 循環(huán)。



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

TA貢獻1785條經(jīng)驗 獲得超8個贊

這真是一個好項目!您已經(jīng)正確地發(fā)現(xiàn)了導致問題的模塊,這很好。在 add_faq() 中,您將錯誤的變量設(shè)置為全局變量,此處已修復:


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條經(jīng)驗 獲得超4個贊

所以我覺得自己很愚蠢,但我在發(fā)布后大約 20 分鐘發(fā)現(xiàn)了我的錯誤。感謝 Viggy 的回復。在閱讀和編碼 3 個多小時后,我有點精疲力盡,所有東西都模糊不清,我感到很沮喪,哈哈。



查看完整回答
反對 回復 2023-04-25
  • 3 回答
  • 0 關(guān)注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號