From 464bdaefdc04628aa1c75d1d00738a76994e16ef Mon Sep 17 00:00:00 2001 From: "Jasper Dahil (dahilj)" Date: Tue, 31 Oct 2017 20:53:41 +0000 Subject: [PATCH] This code deals with "what is ... " question What's in it: 2 functions: - determineUserInput( ) - This will be built bigger as it holds which questions are said - reformatSentence( ) - This formats the user's inputted sentence to just an equation. - The format happens in 2 ways: - the equation has symbols eg. 1 + 1 * 2 - the equation has words eg. 1 plus 1 times 2 the "what" part Here's how the code works: on the bottom, under testing zone, the code asks for the user's input. then, the user's input is passed to the determineUserInput() function (i only worked on the "what" side so ... that's what im gonna focus on) - the program executes the "what" side (you can see it from the code above) - it checks if the "sentence" has numbers - asking questions like "What is 1 + 1" - it checks if the user used symbols or words - the "sentence" and what was user (symbols or words) are passed to the reformatSentence() function - the function checks which one was used - the function returns the "equation" - the function, determineUserInput(), returns answer = the answer as float - print(answer) HOWEVER, something is not right and I've been staring at my computer to find the mistake .... help me ;-; --- "\"what\"Recog" | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 "\"what\"Recog" diff --git "a/\"what\"Recog" "b/\"what\"Recog" new file mode 100644 index 0000000..d94d4ac --- /dev/null +++ "b/\"what\"Recog" @@ -0,0 +1,69 @@ +operatorList = ["+","-","*","/","plus","minus","times","divided"] +numberList = ["1","2","3","4","5","6","7","8","9","0"] + +def determineUserInput(sentence): + """determines what the user asks and responses accordingly""" + sentence = sentence.lower() + if "what" in sentence: # ------------- execute the "what is" code here --------------- + numberQuestion = False + for i in sentence: + if i in numberList: #checks whether the question involves numbers, like 1 + 1 + numberQuestion = True + + if numberQuestion == True: + operatorType = "" + for i in sentence: + if i in operatorList[0:4]: #checks what type is used - Symbols or Words + operatorType = "symbols" + elif i in operatorList[4:len(operatorList)]: + operatorType = "words" + + equationReformated = reformatSentence(sentence,operatorType) + answer = eval(equationReformated) + return answer + + else: + return "null" + + +def reformatSentence(sentence,operatorType): + """reformats the sentence so we're left with just the equation""" + equation = "" + char = "" + if operatorType == "symbols": # this part runs if the equation is using + - * / + for i in sentence: + if i not in "what is ": + i = char + if char == "^": + equation = equation + "**" + else: + equation = equation + char + elif operatorType == "words": # this part runs if the equation uses words, like "plus" + for u in sentence: + if u not in "what is ": + u = char + if char != " ": + equation = equation + char + for part in equation: + if part == "plus": + equation = equation + "+" + elif part == "dividedby": + equation = equation + "/" + elif part == "times" or i == "multipliedby": + equation = equation + "*" + elif part == "tothepowerof": + equation = equation + "**" + elif part == "minus": + equation = equation + "-" + else: + equation = equation + part + return equation + + +# ---------------------------------------- +# testing zone +# ---------------------------------------- + +userInput = input("Input a question: \n") + +print(determineUserInput(userInput))