Reputation: 99
I am relatively new to Python and programming in general. At the moment, I am doing Repl.it student courses. The statement/instruction is as follows:
Write a program that solves a linear equation
ax = b
in integers. Given two integers,a
andb
, wherea
may be zero, print a single integer root if it exists and print "no solution" or "many solutions" otherwise.Example input:
a = 1
,b = -2
Example output:
-2
My code so far looks like this:
a = int(input())
b = int(input())
if a==0:
print("many solutions")
elif (b == 0):
print (b)
elif (a!=0):
x=int(b/a)
if x!=0:
print(x)
elif x==0:
print("no solution")
It fails when a = 0 and b = 7. I do not know why though. Any answers will be much appreciated.
Edit: Thanks for the answers, they were helpful. I have managed to come up with a solution that worked.
Upvotes: 0
Views: 5047
Reputation: 99
I found this to work properly thanks to some of the comments.
a = int(input())
b = int(input())
if a == 0:
if b == 0:
print('many solutions')
else:
print('no solution')
elif b % a == 0:
print(b // a)
else:
print('no solution')
Upvotes: 1