当我按run时,Python代码没有输入。我怎样才能解决这个问题?
这是我的家庭作业: 这是我的代码:当我按run时,Python代码没有输入。我怎样才能解决这个问题?,python,Python,这是我的家庭作业: 这是我的代码: from math import gcd class Rational: def __init__ (self, numerator, denominator): self.numerator = numerator self.denominator = denominator red = self.reduce() self.numerator = red.numerator
from math import gcd
class Rational:
def __init__ (self, numerator, denominator):
self.numerator = numerator
self.denominator = denominator
red = self.reduce()
self.numerator = red.numerator
self.denominator = red.denominator
def add(self, other):
self.numerator = self.numerator * other.denominator + other.numerator * self.denominator
self.denominator = self.denominator * other.denominator
def mul(self, other):
self.numerator = self.numerator * other.numerator
self.denominator = self.denominator * other.denominator
def reduce(self):
if self.numerator == 0 :
return 0
nd_gcd = gcd(self.denominator%self.numerator,self.numerator)
if nd_gcd == 1:
return self
return Rational(int(self.numerator//nd_gcd), int(self.denominator//nd_gcd))
但当我单击run时,没有输出。我不知道怎么修理它。任何帮助都将不胜感激 您需要实例化您的类。我还添加了一个str函数,用于打印类:
from math import gcd
class Rational:
def __init__ (self, numerator, denominator):
self.numerator = numerator
self.denominator = denominator
red = self.reduce()
self.numerator = red.numerator
self.denominator = red.denominator
def __str__(self):
return str(self.numerator) + "/" + str(self.denominator)
def add(self, other):
self.numerator = self.numerator * other.denominator + other.numerator * self.denominator
self.denominator = self.denominator * other.denominator
def mul(self, other):
self.numerator = self.numerator * other.numerator
self.denominator = self.denominator * other.denominator
def reduce(self):
if self.numerator == 0 :
return 0
nd_gcd = gcd(self.denominator%self.numerator,self.numerator)
if nd_gcd == 1:
return self
return Rational(int(self.numerator//nd_gcd), int(self.denominator//nd_gcd))
a = Rational(2, 5)
b = Rational(2, 7)
a.add(b)
print(a)
您还应该在相加和相乘后减少。您只声明了函数和类。您没有实例化它。所以就没有输出了。@Soban我不是那么聪明,而且我真的不擅长Python。我该怎么做?你希望得到什么样的输出?你为什么期望产出?您的任务描述没有说它期望或应该产生任何输出。@Mistermiagi我认为说明说它应该以分数的简化形式输出答案。据我所知(搜索图片而不是文本不是最佳选择),这里显示的说明没有说任何这类事情。