没有 NumPy 的线性方程

Linear equation without NumPy

我对 Python 和一般编程还比较陌生。目前,我正在做 Repl.it 学生课程。 statement/instruction如下:

Write a program that solves a linear equation ax = b in integers. Given two integers, a and b, where a 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

到目前为止我的代码如下所示:

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")

当 a = 0 和 b = 7 时失败。我不知道为什么。任何答案将不胜感激。

编辑:感谢您的回答,它们很有帮助。我设法想出了一个有效的解决方案。

由于一些评论,我发现它可以正常工作。

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')