Python Forum
It worked now it doesnt...ugh
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
It worked now it doesnt...ugh
#1
Can someone look at this and tell me why this does not work....I know it is something simple as it worked 1 min and not the next. picharm is not showing any errors

print("--------------------------")
print("Frequently Asked Questions")
print("--------------------------")


menu = """
1: Add a Question
2: Delete a Question
3: List all FAQ
4: Exit
"""

FAQ = {}


done = False

while not done:

    print(menu)
    option = input("Please enter your selection: ")

    if option == 1:
        question = input("\nEnter a question to be added: ")
        answer = input("\nThe answer to the question is: ")
        FAQ[question] = answer
        print('Your FAQ has been added. ')

    elif option == 2:
        item = input("Enter question to be deleted: ")
        if item in FAQ: del(FAQ[item])
        else:
            print("That question is not in the dictionary! ")

    elif option == 3:
        for item in FAQ:
            print("\nQuestion: ", item)
            print("Answer: ", FAQ[item])
            print()

    elif option == 4:
        done = True
        print('All entries added to dictionary. \nSession is closed! ')
Reply
#2
input returns a string, you are comparing to integers. Either int() the input, or compare to strings like '1'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Aug-07-2019, 05:15 PM)ichabod801 Wrote: input returns a string, you are comparing to integers. Either int() the input, or compare to strings like '1'.

thank you sir I did remove the int last night. works good thank you
Reply
#4
I am trying to make my dictionary produce an error message stating "FAQ already exists" not working. I think the comand is right just maybe not the placement

    if option == 1:
        question = input("\nEnter a question to be added: ")
        answer = input("\nThe answer to the question is: ")
        FAQ[question] = answer
        print('Your FAQ has been added. ')
        item = question
            if item in FAQ:
                print('FAQ already exists')
Reply
#5
Note that FAQ[question] = answer puts question into FAQ. You want to test to see if it's already in there before you put it in there, otherwise the test will always be True.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
The if statement needs to be after the question is asked but before the question is added to the dictionary, otherwise it will always say it already exists.
Reply
#7
You might want to consider version control (with, e.g. Git) to keep track of changes you make to your source code.
Reply
#8
(Aug-07-2019, 06:43 PM)Yoriz Wrote: The if statement needs to be after the question is asked but before the question is added to the dictionary, otherwise it will always say it already exists.

when i move the if statement I get all kinds of errors below in the other options.

Quote:Note that FAQ[question] = answer puts question into FAQ. You want to test to see if it's already in there before you put it in there, otherwise the test will always be True.
I am not quite getting this
Reply
#9
question = input("\nEnter a question to be added: ")
if question in FAQ:
    print('FAQ already exists')
Reply
#10
(Aug-07-2019, 07:10 PM)Yoriz Wrote:
question = input("\nEnter a question to be added: ")
if question in FAQ:
    print('FAQ already exists')

I am getting illegal target for variable annotation on the third option

menu = """
1: Add a Question
2: Delete a Question
3: List all FAQ
4: Exit
"""

FAQ = {}


done = False
print(menu)
option = int(input("Please enter your selection: "))

while not done:

    if option == 1:
        question = input("\nEnter a question to be added: ")
        answer = input("\nThe answer to the question is: ")
        if question in FAQ:
            print('FAQ already exists')
        FAQ[question] = answer
        print('Your FAQ has been added. ')
        item = question        

    elif option == 2:
        item = input("Enter question to be deleted: ")

        if item in FAQ:
            del(FAQ[item])
                print('FAQ deleted')
            else:
                print("That question is not in the dictionary! ")
    elif option == 3:
        for item in FAQ:
            print("\nQuestion: ", item)
            print("Answer: ", FAQ[item])
            print()

    elif option == 4:
        done = True
        print('All entries added to dictionary. \nSession is closed! ')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  im not sure what ive done wrong code doesnt run dgizzly 3 1,357 Nov-16-2022, 03:02 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020