Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sage program question
#1
Hi , I have a problem with the following code (it's fairly simple I guess) , when I enter "what is love?" I get one of the random responses back , and before I managed to let it do this, I got a random response + the intended response to this question(because I made a questionmark the (I have no idea what it is called)).
How can I let python check for the intended input in a practical manner?
Sorry for the strange way of asking this question , I'm new and don't yet know all the names..
#Sage Program

opening = print("Greetings! I am the mighty sage , capable of answering all of your questions!")
opening
def sageprogram():   
    questionmark = """?""" 
    haddaway = """What is love?"""
    question = input("What is your question? : ")
    print("You ask me : ",question)
    answers = ["You know the answer to that already , don't you?",
               "I would focus my thoughts on something else...",
               "The probabilities are in your favor!",
               "It is close to certainty.",
               "Someone you would not expect can be most helpful about this.",
               "Baby don't hurt me! don't hurt me! no more!"]
    def AskQuestionAgain():
        question2 = input("\nWould you like to ask another question to the mighty sage?(yes/no) : ")
        if "yes" in question2:
            sageprogram()
        elif "no" in question2:
            print("Ok, bye.")
            input('\n')
        else:
            print("Only answer in yes or no , please.")
            AskQuestionAgain()
        AskQuestionAgain()
    
    import random
 
    if questionmark in question:
       print(random.choice(answers[:4]))
       AskQuestionAgain()
    if haddaway in question:
      print (answers[5])
      AskQuestionAgain
    else:
        print("I am a sage who can only read questions! ASK AGAIN!")
        sageprogram()
sageprogram()
Reply
#2
First some house cleaning.
You don't need line 4. Lines 6 & 7 can be written as
questionmark = "?"
haddaway = "What is love?"
Quote:How can I let python check for the intended input in a practical manner?

Usually you can do this with the "try/except" statement, for example:

answer = ["yes", "no"]

while True:
    try:
        response = input("Enter your response: ")
        if response in answer:
            print("Correct response")
            break
        else:
            print("Wrong response, try again")
            continue
    except ValueError as err:
        print("There was an error {}".format(err))

print("Done")
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
The first step to solving your problem, is fixing your code so it will actually run. Line 35, for example, literally does nothing (parentheses are needed to call a function).
And please, just make two functions instead of nesting one inside of another. There are valid reasons to have functions within functions, but this is not one of them.
Likewise, there are valid reasons for a function to call itself, but this is not one of those times. A while loop is much better (for your mind to wrap around what's happening, in addition to the stack and memory management).
You also have "import random" at a random point, that should be the very first line of your file.

Once you've organized it a little better following these tips, your code will be much easier to add new things to. As it stands, it is like a descent into madness, the threads of your mind unraveling before us, in neat little lines leading into a dark abyss from which nothing escapes.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 504 Nov-19-2023, 06:31 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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