Skip to content
Permalink
Browse files
Fixed small bugs, cleaned code
- Fixed a small bug showing the error messages

- Removed unnecessary code
  • Loading branch information
mateussa committed Nov 24, 2017
1 parent 9bb8bc4 commit 91ca15bbee9f0aa85dd4bfceab359cde2fda5e47
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 41 deletions.
@@ -235,19 +235,19 @@ def getQuestion(category, difficulty, nrQuestions = 1, qSource = "OpentDB"): # A


if ("Error" in questionSet): # Return every thing as a list to keep the same structure as getting multiple questions if ("Error" in questionSet): # Return every thing as a list to keep the same structure as getting multiple questions
if (questionSet[1] == 0): if (questionSet[1] == 0):
return(["Error", "There was an error opening the URL"]) return([("Error", "There was an error opening the URL")])
elif (questionSet[1] == 1): elif (questionSet[1] == 1):
return(["Error", "Error reading JSON data"]) return([("Error", "Error reading JSON data")])
elif (questionSet[1] == 2): elif (questionSet[1] == 2):
return(["Error", "The number of questions requested not valid"]) return([("Error", "The number of questions requested not valid")])
elif (questionSet[1] == 3): elif (questionSet[1] == 3):
return(["Error", "Category not valid"]) return([("Error", "Category not valid")])
elif (questionSet[1] == 4): elif (questionSet[1] == 4):
return(["Error", "Difficulty not valid"]) return([("Error", "Difficulty not valid")])
elif (questionSet[1] == 5): elif (questionSet[1] == 5):
return(["Error", "No results were found"]) return([("Error", "No results were found")])
elif (questionSet[1] == 6): elif (questionSet[1] == 6):
return(["Error", "Invalid parameter"]) return([("Error", "Invalid parameter")])
elif (questionSet[1] == 7): elif (questionSet[1] == 7):
import os import os


@@ -256,7 +256,7 @@ def getQuestion(category, difficulty, nrQuestions = 1, qSource = "OpentDB"): # A
os.remove("./data/token.jeff") os.remove("./data/token.jeff")
questionSet = getOpentDB(category, difficulty, nrQuestions) questionSet = getOpentDB(category, difficulty, nrQuestions)
if ("Error" in questionSet): if ("Error" in questionSet):
return(["Error", "Token not found or all available question were used"]) return([("Error", "Token not found or all available question were used")])


return(questionSet) return(questionSet)


@@ -57,7 +57,7 @@ def askSomething(answerType, sendMessages, noAnswers, defaultAnswer): # Andre
break break


if (answer[1] == 2): # If the user asked a question if (answer[1] == 2): # If the user asked a question
if (type(answer[0]) == tuple): # Send two messages, the text and the link of the google search if (type(answer[0]) == tuple): # Send two messages, i.e. the text and the link of the google search
sendMessage(answer[0][0], False) sendMessage(answer[0][0], False)
sendMessage(answer[0][1], False) sendMessage(answer[0][1], False)
else: else:
@@ -68,7 +68,7 @@ def askSomething(answerType, sendMessages, noAnswers, defaultAnswer): # Andre
elif (answer[1] == 5): # If the user doesn't want to answer elif (answer[1] == 5): # If the user doesn't want to answer
sendMessage("You should, it would be more fun!") sendMessage("You should, it would be more fun!")
noAnswers.pop(len(noAnswers) - 1) noAnswers.pop(len(noAnswers) - 1)
else: elif (answer[1] != answerType):
sendMessage("Sorry I didn't undertood that.", False) sendMessage("Sorry I didn't undertood that.", False)
sendMessage("Can you repeat please.") sendMessage("Can you repeat please.")
noAnswers.pop(len(noAnswers) - 1) noAnswers.pop(len(noAnswers) - 1)
@@ -94,13 +94,12 @@ def oneQuestion(qType): # Andre
["You can choose a category from the list above, like 'Music'.", ["You can choose a category from the list above, like 'Music'.",
"Hmmm, I see you are afraid of making a mistake."], "Any") "Hmmm, I see you are afraid of making a mistake."], "Any")
else: else:
receivedMessage = "" receivedMessage = "" # Create the variable anyway, because it can be another type of question


# Get a question and answers, from the user choice # Get a question and answers, from the user choice
questionSet = getQuestion(receivedMessage, "", 1, qType)[0] questionSet = getQuestion(receivedMessage, "", 1, qType)[0]
if ("Error" in questionSet): if ("Error" in questionSet):
sendMessage(questionSet[1], False) sendMessage("{}: {}".format(questionSet[0], questionSet[1]), False)
sendMessage("Lets try again.", False)
return(questionSet) return(questionSet)


# If question set of Multiple type # If question set of Multiple type
@@ -120,24 +119,10 @@ def oneQuestion(qType): # Andre
["You can choose one of the options.", ["You can choose one of the options.",
"Hmmm, I see you are afraid of making a mistake."], "X") "Hmmm, I see you are afraid of making a mistake."], "X")


# If question set of Birthday type # If question set of Birthday or History or Quote type
elif (questionSet["Type"] == "Birthday"): elif (questionSet["Type"] in ["Birthday"], "History", "Quote"):
receivedMessage = askSomething(0, [questionSet["Question"]],
["You can try a random one if felling lucky.",
"Hmmm, I see you are afraid of making a mistake."], "X")
questionSet[receivedMessage.upper()] = receivedMessage

# If question set of History type
elif (questionSet["Type"] == "History"):
receivedMessage = askSomething(0, [questionSet["Question"]], receivedMessage = askSomething(0, [questionSet["Question"]],
["You can try a random one if felling lucky.", ["You can try a random answer if felling lucky.",
"Hmmm, I see you are afraid of making a mistake."], "X")
questionSet[receivedMessage.upper()] = receivedMessage

# If question set of Quote type
elif (questionSet["Type"] == "Quote"):
receivedMessage = askSomething(0, [questionSet["Question"]],
["You can try a random one if felling lucky.",
"Hmmm, I see you are afraid of making a mistake."], "X") "Hmmm, I see you are afraid of making a mistake."], "X")
questionSet[receivedMessage.upper()] = receivedMessage questionSet[receivedMessage.upper()] = receivedMessage


@@ -157,16 +142,12 @@ def quizChallange(nrQuestions): # Andre
with the lenght of the choosen input ''' with the lenght of the choosen input '''
if (nrQuestions < 1 or nrQuestions > 50): if (nrQuestions < 1 or nrQuestions > 50):
sendMessage("For a Quiz challenge ou have to choose between 2 and 50 quesitons.") sendMessage("For a Quiz challenge ou have to choose between 2 and 50 quesitons.")
return(True) return(nrQuestions)


cat = getCategories(True, 3) cat = getCategories(True, 3)
if ("Error" in cat): if ("Error" in cat):
sendMessage("{}: {}".format(cat[0], cat[1]), False) sendMessage("{}: {}".format(cat[0], cat[1]), False)
if (cat[1] == 7): return(cat)
sendMessage("Lets try again.")
return(True)
else:
return(False)


receivedMessage = askSomething(-1, ["Pick a subject.", cat[0], cat[1], cat[2]], receivedMessage = askSomething(-1, ["Pick a subject.", cat[0], cat[1], cat[2]],
["You can choose a category from the list above, like 'Music'.", ["You can choose a category from the list above, like 'Music'.",
@@ -176,11 +157,10 @@ def quizChallange(nrQuestions): # Andre
# Get a question and answers, from the user choice # Get a question and answers, from the user choice
qChaSet = getQuestion(receivedMessage, "", nrQuestions) qChaSet = getQuestion(receivedMessage, "", nrQuestions)
if ("Error" in qChaSet): if ("Error" in qChaSet):
sendMessage(qChaSet[1], False) sendMessage("{}: {}".format(qChaSet[0], qChaSet[1]), False)
sendMessage("Lets try again.", False) return(qChaSet)
return (True)


Score = 0 Score = 0 # Initialize the variable to keep track of the right answers


for questionSet in qChaSet: for questionSet in qChaSet:
# If question set of multiple type # If question set of multiple type
@@ -231,7 +211,7 @@ def funFacts(): # Andre







# Start server #
#################### ####################
# # # #
# CONNECTION # # CONNECTION #

0 comments on commit 91ca15b

Please sign in to comment.