Stop the program from asking the same thing twice? - python

Making a math quiz in python with random equations. I'm not sure how to stop the program from asking the same thing twice?
The program asks ten questions from a list of add, subtract or multiply, each using random numbers. I've managed to make the ten questions random, but I'm not sure how to stop it from choosing the same two numbers twice? For example, it would choose 1+3 for one question, but it will ask the same question multiple times after. Here is the code:
import random
#Asks for name
name = input("What's your name?")
#Stops user from entering invalid input when entering their class
classchoices = ["A","B","C"]
classname = input("What class are you in?")
while classname not in classchoices:
classname = input("Not a valid class, try again:")
print(name, ",", classname)
print("Begin quiz!")
questions = 0
def add(a,b):
addQ = int(input(str(a) + "+" + str(b) + "="))
result = int(int(a) + int(b))
if addQ != result:
print ("Incorrect!", result)
else:
print("Correct")
a = random.randint(1,12)
b = random.randint(1,12)
def multiply(a,b):
multQ = int(input(str(c) + "X" + str(d) + "="))
results = int(int(c) * int(d))
if multQ != results:
print ("Incorrect! The answer is", results)
else:
print("Correct")
c = random.randint(1,12)
d = random.randint(1,12)
def subtract(a,b):
subQ = int(input(str(e) + "-" + str(f) + "="))
resultss = int(int(e) - int(f))
if subQ != resultss:
print ("Incorrect! The answer is", resultss)
else:
print("Correct")
e = random.randint(1,12)
f = random.randint(1,12)
while questions in range(10):
Qlist = [add, subtract, multiply]
random.choice(Qlist)(a,b)
questions += 1
if questions == 10:
print ("End of quiz")
Any help would be appreciated, thanks.

The issue here is that you are generating random numbers at the start of the program, but not regenerating them every time a question is asked: your add function will always use a and b, multiply will always use c and d, and subtract will always use e and f for all of their respective calculations, the values of which never change.
In addition, the parameters you are inputting are of no value for multiply and subtract as you are just disregarding them and using c, d and e, f respectively without note of the parameters inputted.
In order to remedy both these issues, I would place the random generation of the numbers inside the while loop and make the functions use the correct inputted parameters for calculations.
Moreover, the while iteration is a bit redundant a simple for questions in range(10) without the extra bits is much more straightforward. Hence the questions variable is useless.
With all that in mind, here is the rewritten code.
import random
#Asks for name
name = input("What's your name?")
#Stops user from entering invalid input when entering their class
classchoices = ["A","B","C"]
classname = input("What class are you in?")
while classname not in classchoices:
classname = input("Not a valid class, try again:")
print(name, ",", classname)
print("Begin quiz!")
def add(a,b):
addQ = int(input(str(a) + "+" + str(b) + "="))
result = int(int(a) + int(b))
if addQ != result:
print ("Incorrect!", result)
else:
print("Correct")
def multiply(a,b):
multQ = int(input(str(a) + "X" + str(b) + "="))
results = int(int(a) * int(b))
if multQ != results:
print ("Incorrect! The answer is", results)
else:
print("Correct")
def subtract(a,b):
subQ = int(input(str(a) + "-" + str(b) + "="))
resultss = int(int(a) - int(b))
if subQ != resultss:
print ("Incorrect! The answer is", resultss)
else:
print("Correct")
for i in range(10):
Qlist = [add, subtract, multiply]
random.choice(Qlist)(random.randint(1, 12),random.randint(1, 12))
print ("End of quiz")
The program could be refined further by creating a function that manages the printing and checking of the result dependent on a third parameter that dictates what operation it should perform.

for each problem you have asked, you can add it to a set of "asked_questions" and use "in" method to test if already asked, and if yes, generate a new question.
not sure if it's the most efficient method, but it definitely works, and more than enough for your little application.

Related

This Python quiz is driving me crazy

I'm new to StackOverflow (1st time posting) and new to coding with python. Currently enrolled in a course through Udacity. I'm having a very hard time with a project we were given for this course and decided to come here to see if anyone could help.
The project is to create a quiz with 4 blanks that need to be answered correctly by the player. It's required to have the quiz print out with the correct answer, but I'm having a very hard time getting this to print out correctly.
My code is below. Would appreciate any help or advice I can get on this.
Thanks!
easy_quiz = "If you ever get stuck, check out the __1__ for common
problems students face when attempting this project. If you need
additional help, you can schedule a 1:1 appointment with one of our
__2__ to get you un-stuck. This project should be __3__. If at any time
it becomes not fun, take a step back, deep breath, and ask for __4__!.
\n\n"
easy_answers = ["forums", "mentors", "fun", "help"]
medium_quiz = "Game must have 3 or more levels and each level contains 4 or more __1__ to fill in. Immediately after running the program, user is prompted to select a difficulty level from easy / __2__ / hard. Once a level is selected, game displays a fill-in-the-blank and a prompt to fill in the first one. When player guesses __3__, new prompt shows with correct answer in the previous blank and a new prompt for the next blank. When player guesses __4__, they are prompted to try again. \n"
medium_answers = ["blanks", "medium", "correctly", "incorrectly"]
hard_quiz = "__1__ are used as __2__ to automate tasks which are likely to be repeated. Functions produce the appropriate output (typically with a __3__ statement) from the appropriate input (function parameters). Your code should take advantage of __4__ and variable names should reflect the values they store. \n"
hard_answers = ["Functions", "tools", "return", "variables"]
blanks = ["__1__", "__2__", "__3__", "__4__"]
difficulty = raw_input("\nChoose your difficuty level = easy, medium, or hard? ")
print ""
if difficulty == "easy":
quiz = easy_quiz
answers = easy_answers
print "You chose easy!\n\nYou will have 5 guesses to fill in each blank. Good Luck!!\n \n" + easy_quiz
elif difficulty == "medium":
quiz = medium_quiz
answers = medium_answers
print "You chose medium!\n\nYou will have 5 guesses to fill in each blank. Good Luck!!\n \n" + medium_quiz
elif difficulty == "hard":
quiz = hard_quiz
answers = hard_answers
print "You chose hard!\n\nYou will have 5 guesses to fill in each blank. Good Luck!!\n \n" + hard_quiz
def word_in_pos(word, parts_of_speech):
for pos in parts_of_speech:
if pos in word:
return pos
return None
def play_game(quiz, parts_of_speech):
replaced = []
i = 0
quiz = quiz.split()
for word in quiz:
replacement = word_in_pos(word, parts_of_speech)
if replacement != None:
user_input = raw_input("Type an answer for: " + replacement + " " )
word = word.replace(replacement, user_input)
replaced.append(word)
guesses = 0
while user_input != answers[i]:
guesses = guesses + 1
print "Incorrect, try again \n" + " ".join(replaced)
user_input = raw_input("Type an answer for: " + replacement + " ")
if guesses == 4:
return "\nGame Over! Better luck next time. \n"
print "Correct \n" + " ".join(replaced)
i = i + 1
word = word.replace(replacement, user_input)
replaced.append(word)
else:
replaced.append(word)
replaced = " ".join(replaced)
return replaced
print play_game(quiz, blanks)
Here is a working version of your play_game() method:
def play_game(quiz, parts_of_speech):
replaced = []
i = 0
quiz = quiz.split()
for word in quiz:
replacement = word_in_pos(word, parts_of_speech)
if replacement is not None:
user_input = raw_input("Type an answer for: " + replacement + " " )
guesses = 0
while user_input != answers[i]:
guesses = guesses + 1
if guesses == 5:
return "\nGame Over! Better luck next time. \n"
print "Incorrect, try again \n" + " ".join(replaced) + " " + replacement
user_input = raw_input("Type an answer for: " + replacement + " ")
replaced.append(user_input)
print "Correct \n" + " ".join(replaced)
i = i + 1
else:
replaced.append(word)
replaced = " ".join(replaced)
return replaced
The main change is to delay modifying the replaced list until the correct answer has been given. That simplifies a lot of the code, eliminating the need for the word variable.

How do you change a variable in a function to then be used in the main code?

I am quite new to Python and I am having some trouble figuring out the following:
import random
import sys
print("Welcome to this Maths quiz.")
playerName = str(input("Please enter your name: "))
playerAge = int(input("Please enter your age: "))
if playerAge < 11:
print("This quiz is not for your age.")
sys.exit(0)
else :
print("Great! Let's begin.\n")
quizQuestions = ["9(3+8)", "7+9*8", "(9+13)(9-5)", "50*25%", "104-4+5*20"]
quizAnswers = ["99", "79", "88", "12.5", "0"]
quizSync = list(zip(quizQuestions, quizAnswers))
random.shuffle(quizSync)
quizQuestions, quizAnswers = zip( * quizSync)
questionNumber = 1
quizScore = 0
def displayQuestion(quizQuestions, quizAnswers, questionNumber, quizScore):
print("Question " + str(questionNumber) + ": " + quizQuestions[questionNumber - 1] + "\n")
questionAnswer = str(input())
if questionAnswer == quizAnswers[questionNumber - 1]:
print("\nCorrect!\n")
quizScore += 1
else :
print("\nIncorrect! The answer is: " + quizAnswers[questionNumber - 1] + "\n")
while questionNumber < 6:
displayQuestion(quizQuestions, quizAnswers, questionNumber, quizScore)
questionNumber += 1
print("You have a total score of: "+str(quizScore))
I would like the variable "quizScore" in the function "displayQuestion" to increase by one if the player gets a question right. However, after the quiz is finished, the print function at the end always prints the score is 0 even if the player gets questions right.
You have to declare it as a global variable inside the function so that it can modify the variable in the global scope
def displayQuestion(quizQuestions, quizAnswers, questionNumber):
global quizScore
...
quizScore += 1
That being said, you should generally avoid global variables if you can and try to redesign your program to either pass the variables along as arguments and return values, or use a class to encapsulate the data.
Although this won't be the shortest answer, which is to use another global variable. It instead will show you how to avoid using global variables (which are considered harmful) by using Object Oriented Programming (OOP). To accomplish this, most of the code in your question can be encapsulated into a single class named MathQuiz below.
Besides getting rid of almost all global variables, it also provides a usable template for you to create any number of independent math quizzes.
import random
import sys
class MathQuiz:
def __init__(self, questions, answers):
quizSync = list(zip(questions, answers))
random.shuffle(quizSync)
self.quizQuestions, self.quizAnswers = zip(*quizSync)
self.quizScore = 0
print("Welcome to this Maths quiz.")
self.playerName = str(input("Please enter your name: "))
self.playerAge = int(input("Please enter your age: "))
if self.playerAge > 10:
print("Great! Let's begin.\n")
else :
print("This quiz is not for your age.")
sys.exit(0)
def run(self):
for questionNumber in range(len(self.quizQuestions)):
self._displayQuestion(questionNumber)
print("You have a total score of: " + str(self.quizScore))
def _displayQuestion(self, questionNumber):
print("Question " + str(questionNumber) + ": "
+ self.quizQuestions[questionNumber-1]
+ "\n")
questionAnswer = str(input())
if questionAnswer == self.quizAnswers[questionNumber-1]:
print("\nCorrect!\n")
self.quizScore += 1
else :
print("\nIncorrect! The answer is: "
+ self.quizAnswers[questionNumber-1]
+ "\n")
quiz = MathQuiz(["9(3+8)", "7+9*8", "(9+13)(9-5)", "50*25%", "104-4+5*20"],
["99", "79", "88", "12.5", "0"])
quiz.run()

Python: ' ' is not defined

Here is my code:
# This program makes the robot calculate the average amount of light in a simulated room
from myro import *
init("simulator")
from random import*
def pressC():
""" Wait for "c" to be entered from the keyboard in the Python shell """
entry = " "
while(entry != "c"):
entry = raw_input("Press c to continue. ")
print("Thank you. ")
print
def randomPosition():
""" This gets the robot to drive to a random position """
result = randint(1, 2)
if(result == 1):
forward(random(), random())
if(result == 2):
backward(random(), random())
def scan():
""" This allows the robot to rotate and print the numbers that each light sensors obtains """
leftLightSeries = [0,0,0,0,0,0]
centerLightSeries = [0,0,0,0,0,0]
rightLightSeries = [0,0,0,0,0,0]
for index in range(1,6):
leftLight = getLight("left")
leftLightSeries[index] = leftLightSeries[index] + leftLight
centerLight = getLight("center")
centerLightSeries[index] = centerLightSeries[index] + centerLight
rightLight = getLight("right")
rightLightSeries[index] = rightLightSeries[index] + rightLight
turnRight(.5,2.739)
return leftLightSeries
return centerLightSeries
return rightLightSeries
def printResults():
""" This function prints the results of the dice roll simulation."""
print " Average Light Levels "
print " L C R "
print "========================="
for index in range(1, 6):
print str(index) + " " + str(leftLightSeries[index]) + " " + str(centerLightSeries[index]) + " " + str(rightLightSeries[index])
def main():
senses()
pressC()
randomPosition()
scan()
printResults()
main()
So, I am getting this error when I run my program.
NameError: global name 'leftLightSeries' is not defined
I understand that I must be doing something wrong related to the return statement. I'm not sure if I can only return one variable at the end of a user-defined function. If that were to be true, then I should probably separate the scan(): function. Anyways, I would appreciate any help on how to fix this error. Also, this is the result that I am looking for when I successfully complete my program:
Click Here
I am looking to complete the average values like the picture shows, but I am not worried about them at this point, only the list of values from the light sensors. I do not need to reach those exact numbers, the numbers will vary in the simulator.
If you want to return multiple items from scan(), don't use three separate return statements. Instead, do this:
return leftLightSeries, centerLightSeries, rightLightSeries
Also, when you call the function, you have to assign variable(s) to the returned values; it won't automatically create new local variables with the same names. So in main, call scan() like this:
leftLightSeries, centerLightSeries, rightLightSeries = scan()

Integer Validation Python

I need to add a validation whilst in a while loop.
However when I use this validation it doesn't work and instead only comes up with the error message saying I haven't used a base 10/an integer when I want it to come up with the validation error message and let the user try again.
I don't know if having it in a while loop makes the validation I use any different, does it?
Also do I need to change this "def inputNumber(message):" to what my input is stored as?
And this "userInput = int(input(message))" to what my input is stored as?
import time
import random
question = 0
score = 0
name = input("What is your full name?")
print ("Hello " + name, "welcome to The Arithmetic Quiz. Use integers to enter the answer!")
time.sleep(2)
operands1 = list(range(2, 12))
operators = ["+","-","x"]
operands2 = list(range(2, 12))
while question < 10:
operand1 = random.choice(operands1)
operand2 = random.choice(operands2)
operator = random.choice(operators)
def inputNumber(message):
while True:
try:
userInput = int(input(message))
except ValueError:
print("Not an integer! Try again.")
continue
else:
return userInput
break
user_answer =int(input('{} {} {} = '.format(operand1, operator, operand2)))
I doubt you want to have your function definitions within a while loop like you're doing here:
while question < 10:
...
def inputNumber(message):
...
Instead, you can define the function outside the loop and call it x number of times from a loop elsewhere. E.g.
def inputNumber(message):
...
return userInput
while question < 10:
# pick random numbers/operators
...
# call inputNumber() with numbers/operators as message. Return user_answer
user_answer = int(inputNumber('{} {} {} = '.format(operand1, operator, operand2)))
# check if the answer is correct
...
# increment question so it doesn't run infinitely
question += 1
#user6104134 has already solved this problem; however, I'd like to provide an answer for anyone else having similar issues.
Try this solution
import random
import time
question = 0
score = 0
def inputnumber(prompt):
while True:
response = raw_input(prompt)
try:
if isinstance(response, int):
return int(response)
else:
print "Not an integer! Try again."
except ValueError:
print "Not an integer! Try again."
name = raw_input("What is your full name? ")
print ("Hello " + name, "welcome to The Arithmetic Quiz. Use integers to enter the answer!")
time.sleep(2)
operands1 = list(range(2, 12))
operators = ["+", "-", "x"]
operands2 = list(range(2, 12))
while question < 10:
operand1 = random.choice(operands1)
operand2 = random.choice(operands2)
operator = random.choice(operators)
user_answer = int(inputnumber('{} {} {} = '.format(operand1, operator, operand2)))
question += 1
Issues
First, you should declare function definitions outside of your script and call the function by identifier 'inputNumber()'
Also notice the slight change in Try/Except, and the PEP 8 Style Guide compliant formatting.

Python please tell me if my maths answer is right

I wish to add a feature which tells the user when he/she has answered correctly to the random maths questions that are given.
import random
def answers():
correct_answer_P_ = ((str(difficulty_one + difficulty_one))) #P = PLUS, ADDITION +
correct_answer_M_ = ((str(difficulty_one * difficulty_one))) #M = MULTIPLY *
correct_answer_T_ = ((str(difficulty_one - difficulty_one))) #T = TAKE AWAY, MINUS -
def random_symbols():
symbols = random.choice (["+","-","*"])
return symbols
def difficulty_one():
dif_one = random.randrange (1,10,1)
return dif_one
def questions():
question = (str(difficulty_one())) + random_symbols() + (str(difficulty_one())) + " = "
return question
start = input("press start to begin!: ")
if (start == "start"):
print ("here's ten questions, good luck!")
for questions_num in range(1,11):
print ("Question ",questions_num)
input(questions())
if (random_symbols == "+"):
if (dif_one == correct_answer_P_):
print("correct!")
elif(random_symbols == "-"):
if (dif_one == correct_answer_T_):
print("correct!")
elif(random_symbols == "*"):
if (dif_one == correct_answer_M_):
print("correct!")
else:
print("incorrect!")
I tried this from getting some advice from a friend who said that i needed to create variables for each symbol that would be randomly inserted; the variables should compare the users answer and say its correct but it skips all the if statements and goes straight to saying its incorrect.
Any suggestions? Don't be harsh if I'm doing something dumb because I've just started python at the moment.
Quick note the part in this code has been cut out of my original to make it simple for people to see what I'm trying to do.
There's a way easier way of implementing this. Try this instead:
import operator
questions = [(1,2), (4,2), (8,1), (10,100)] # operands for your questions
operators = {"+" : operator.add,
"-" : operator.sub,
"*" : operator.mul}
# a dictionary containing each operator's symbol and the resulting function
# (operator.add(x,y) is equivalent to x+y)
for num, operands in enumerate(questions,start=1):
# iterate through each set of operands, enumerating starting at 1
operator = random.choice(operators)
# return a random symbol from operators
answer = operators[operator](*operands)
# operators[operator] is the function, which we then call with the operands
q_text = "{} {} {} = ?".format(str(operands[0]), operator, str(operands[1]))
print("Question {}".format(str(num)))
print(q_text)
user_answer = input(">>")
if float(user_answer) == answer:
# correct!
else:
# fail!

Categories

Resources