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)
Related
I need to make a function to make that will give me an angle classification like acute right or obtuse and I need it to loop until the user wants to exit the function:
this is my code so far:
side_a = float(input('Enter length of side a: '))
side_b = float(input('Enter length of side b: '))
side_c = float(input('Enter length of side c: '))
def is_valid_triangle(a,b,c):
if a+b>=c and b+c>=a and c+a>=b:
return True
else:
return False
# Function definition for type of triangle
def type_of_triangle(a,b,c):
if a==b and b==c:
print('Triangle is Equilateral.')
elif a==b or b==c or a==c:
print('Triangle is Isosceles.')
else:
print('Triangle is Scalane')
return False
if is_valid_triangle(side_a, side_b, side_c):
type_of_triangle(side_a, side_b, side_c)
else:
print('Tringle is not possible from given sides.')
def triangle_angle (a, b, c, ) :
a = int(input ("enter side a: "))
b = int(input("enter side b : "))
c = int(input("enter side c : "))
I'm assuming you want to know how to constantly loop code.
You can use:
try:
while True:
pass # replace with whatever code you want to run
except KeyboardInterrupt:
print('Exiting...')
Which will constantly loop until the user presses Ctrl+C.
I think this answers your question.
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!
To print strings with math addition numbers in Python, what is the correct way doing something like:
# Read two inputs from users
a = input("Enter your First Number")
b = input("Enter your Second Number")
# perform type conversion
a = int(a)
b = int(b)
print (f"Result: {a}+{b}")
Output:
Enter your First Number10
Enter your Second Number10
Result: 10+10
Desired output: Result: 20
Your current format string Result: {a}+{b} prints out a and b individually as Result: 10+10 and doesn't perform the addition.
To achieve that, you need to change the f-string to f"Result: {a+b}" so that the addition happens within the formatting curly braces and the result gets printed
print (f"Result: {a+b}")
The output will be
Result: 20
a = input("Enter your First Number")
b = input("Enter your Second Number")
# perform type conversion
a = int(a)
b = int(b)
print (f"Result: {a + b}")
You should change
print (f"Result: {a} + {b}")
to
print (f"Result: {a + b}")
# Read two inputs from users
a = int(input("Enter your First Number"))
b = int(input("Enter your Second Number"))
print ("Result: {}".format(a+b))
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.
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")