Making a mathematics quiz in python - python

import random
def mathquiz():
name = str(input("Whats your name?:"))
a = random.randint(1,10)
b = random.randint(1,10)
c = (a * b)
timesby = ("*")
print('what is', + a, timesby, + b)
ans = input("Enter your answer here")
if ans == c:
print("Thats correct")
else:
print ("Incorrect the answer is", + c)
I am trying to create a maths quiz using random numbers and just say the question is 10 * 10 if i enter 100 if will say i have the wrong answer. Everything else works fine just this bit is wrong.

ans and c are of different types. Since you read ans from the console, it is a string, and not a number, while c is calculated through multiplying 2 numbers, so it is a number. there are 2 ways to fix this, convert ans to an int with int(ans) instead of ans, or convert c to a string with str(c) instead of c.

Related

Quadratic Formula Calculator Python

What is wrong with the program below?
#Quadratic Formula Calculator
#Asks the user for a, b, and c
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
d = b**2-4*a*c
#Program output should be in the exact format below:
#Enter a:
#Enter b:
#Enter c:
#The answers are x=3.0 and x=-2.0
#!!!!!!!!
#Put a \n at the end of your input string to the
#quotation marks-> input("Enter a: \n")
#!!!!!!!!
if d < 0:
print ("This equation has no real solution")
elif d == 0:
x = (-b+math.sqrt(b**2-4*a*c))/2*a
print ("This equation has one solutions: "), x
else:
x1 = (-b+math.sqrt((b**2)-(4*(a*c))))/(2*a)
x2 = (-b-math.sqrt((b**2)-(4*(a*c))))/(2*a)
print ("This equation has two solutions: ", x1, " or", x2)
math =
You have a line that's simply math =, and import math is not shown in your code, also when d=0 you forgot to add parenthesis.
First things first.
You are using the math module but you haven't imported it
So import math at the top of the code.
The ,x is outside the print statement
print ("This equation has one solutions: ",x)
Even if you import math, math = will surely over ride it. Also, there is no assignment to it.
Here d is already defined.
d = b**2-4*a*c
No need to do:
(-b+math.sqrt((b**2)-(4*(a*c))))/(2*a)
You can do:
(-b+math.sqrt(d))/(2*a)

Errors pertaining to the "!==" function in Python

I'm currently trying to make a program in Python that basically asks you math problems. For some reason, there's a lot of errors, and I have no idea why, such as the "!==" function, which seems to be incorrect. There might also be some other issues as I've only just started Python. Could someone direct me to how to use the !== function correctly and also my other issue: Making my input for "addition, subtraction, or multiplication" into a possible command?
# import random
import time
choice = 0
counterright = 0
counterwrong = 0
def add_question_to_file(a, b):
with open("wrong_answers.txt", 'a') as f:
f.write(f"{a} {operation} {b}\n")
def test(a,operation,b):
user_input = int(input(f"{a} {operation} {b} = "))
if user_input == a + b:
print("You got it right!")
counterright += 1
while user_input != a + b:
if user_input == a + b:
print("You got it right!")
counterwrong += 1
break
else:
add_question_to_file(a, b)
print("Try Again!")
user_input = int(input(f"{a} + {b} = "))
def get_question():
a = random.randint(1, 10)
b = random.randint(1, 10)
with open("calc_log.txt", 'a') as f:
if a<b: #insures a is always greater than b
a, b = b, a
f.write(f"{a} {operation} {b}\n")
def check_operation():
if operation !==t "+" or "-" or "*"
print("Sorry, that's invalid. You have to choose of the three operations!")
t1=time.perf_counter() #
m = input("What mode would you like to use? Review/Normal. Choose Normal if this is your first time!")
operat ion = input("Which operation would you like to use? +,-,or *?")
if m == "review":
with open("wrong_answers.txt", 'r') as f:
for line in f:
a, operation, b = line.split()
test(int(a), int(b))
elif m == "normal":
num_questions = int(input("How many questions would you like to do? "))
for i in range(num_questions):
print(f"You are on question {i + 1}: ")
get_question()
t2=time.perf_counter() #
print("It took you", t2-t1, "seconds for you to solve this problemset") #
print ("You got",counterright,"correct and",counterwrong,"wrong.")
The reason !== isn't working is because this is not an operator in python. Try using !=. It means not equal too.
The does not equal function in python is !=. The double equal sign (==) is only used when you want to check for equality. I hope this helps!

Multi-digits bug code [duplicate]

This question already has answers here:
Comparing numbers give the wrong result in Python
(4 answers)
Closed 4 years ago.
Very new to programming and decided to get into Python. Sorry in advance if this question is very simple. Experimenting with functions. Got this to run with no errors, but doesn't always work.
I'm pretty sure I'm doing the string replacement wrong? Any explanation or advice would be great.
Noticed it wasn't working when I was comparing a single digit number to a multi digit.
def bigger(a, b):
if a > b:
print ("%s is bigger" % a)
elif a == b:
print ("They are both equal!")
else:
print ("%s is bigger" % b)
a = input("Pick a number: ")
b = input("Pick another number: ")
bigger(a, b)
input by default return String. You need to convert it to numbers. Change the int into float or double, if dealing with decimal numbers
def bigger(a, b):
if int(a) > int(b):
print ("%s is bigger" % a)
elif a == b:
print ("They are both equal!")
else:
print ("%s is bigger" % b)
a = input("Pick a number: ")
b = input("Pick another number: ")
bigger(a, b)
a = input("Pick a number: ")
b = input("Pick another number: ")
input() is always represented as a string and not an integer so in order to run comparisons like if a > b you must first convert it to an integer, like this:
a = int(input("Pick a number: "))
b = int(input("Pick another number: "))
The reason your program works is because Python is comparing the individual sizes of your strings. So "32" starts with a 3 while "122" starts with a 1 so 32 is larger.
>>> '122'<'32'
True
>>> '44'>'1333333'
True

Python "if" statement not working

I was writing a complex program and I was getting an if statement...
(this isn't the complex code, this is just an example)
print("The 24 game will give you four digits between one and nine")
print("It will then prompt you to enter an ewuation one digit at a time.")
import random
a = random.randint(1,9)
b = random.randint(1,9)
c = random.randint(1,9)
d = random.randint(1,9)
print(a,b,c,d)
f=input("Enter one of the above numbers")
if f==a:
print("ok")
elif f != a:
print("No")
No matter what I type it always outputs "NO".
It would work after converting the user input string to a number:
if int(f) == a:
print("ok")

It gives the right answer and sometimes gives the wrong answer

print("Hello, and welcome to the Maths quiz!/n")
#Asks user for name - 05/03/2015
name = input("What is your name?")
#This will import random to generate random functions
import random
#This is a variable for score
#It has been set to 0 at the start of the program
score = 0
#This creates an array containing the mathematical operators
#that this quiz will use
ops = ['+','-','*']
#A loop has been set for 0 - 10
for x in (0,10):
#This is variable has been set to the operator of the equation that
#uses the random function and will choose a random operator from the
#array containing the operators made earlier
op = random.choice(ops)
if op == '+':
left1 = random.randint(1,100)
right1 = random.randint(1,100)
print (str(left1) + op + str(right1))
answer = eval(str(left1) + op + str(right1))
guess = input("")
if guess == answer:
print("Correct!")
score += 1
else:
print ("Incorrect")
elif op == '-':
left2 = random.randint(1,100)
right2 = random.randint(1,100)
print (str(left2) + op + str(right2))
answer1 = eval(str(left2) + op + str(right2))
guess1 = int(input(""))
if answer1 == guess1:
print("Correct!")
score += 1
else:
print("Incorrect")
elif op == '*':
left3 = random.randint(1,100)
right3 = random.randint(1,100)
print (str(left3) + op + str(right3))
answer2 = eval(str(left3) + op + str(right3))
guess2 = input("")
if answer2 == guess2:
print("Correct!")
score += 1
else:
print("Incorrect")
else:
break
print (score)
When I do this, it generates a random quiz that only loops twice, even though I want it to loop 10 times. Also, it sometimes give the right answer and sometimes the wrong answer. For example:
Hello, and welcome to the Maths quiz!/n
What is your name?j
95*3
285
Incorrect
35-46
-11
Correct!
What I want for this to do is generate random arithmetic questions using the addition, subtraction and multiplication operators. Plus, for it to loop 10 times and give a score out of 10 at the end.
This
for x in (0,10):
...
runs the code (...) two times: one time with x set to 0, and a second time with x set to 10.
What you really want is this:
for x in range(10):
...
Then x will be 0, 1, ..., 9 and the code runs 10 times.
You are not always converting the input to an integer.
When op == '*' or op == '+', you try to compare against the string returned from input():
guess2 = input("")
String comparisons to numbers are never equal. For op == '-' you correctly convert the answer to an integer first:
guess1 = int(input(""))
Your loop is broken too; you are looping over a tuple containing two values:
for x in (0, 10):
rather than over a range:
for x in range(0, 10):
You'd be far better off avoiding so much repetition in your code; calculate the expression outcome and ask for the answer in one place; that way there are fewer places to make mistakes.

Categories