我不知道我的代码有什么问题。它不会执行。什么都没有发生,没有错误发生。我想不通。如果有人能告诉我我做错了什么,请告诉我,我将不胜感激。
class Money (object):
def __init__ (self, euro, cent):
self.euro = euro
self.cent = cent
def __str__ (self):
if self.cent >= 100:
r = self.cent / 100
self.cent = self.cent % 100
self.euro = self.euro + r
return ("%d EUR & %d cents") % (self.euro, self.cent)
else:
return ("%d EUR & %d cents") % (self.euro, self.cent)
def changeCent (self):
#100 c = 1 E
cents = self.euro * 100
self.cent = self.cent + cents
return self.cent
def changeSum (self, euros):
#1 E = 100 c
euros = self.cent / 100
self.euro = self.euro + euros
return self.euro
def debt (self, years, rate):
value = Money()
multiply = rate * years * 12 / 100
value.euro = self.euro * multiply
value.cent = self.cent * multiply
if value.cent > 100:
euro_ = value.cent / 100
value.cent = value.cent - 100
value.euro = value.euro + euro_
return value
def main():
x = Money()
x.euro = int(input("Type in your EURO ammount: \n"))
x.cent = int(input("Type in your CENT ammount: \n"))
print (x)
Run Code Online (Sandbox Code Playgroud)
在 python 中,main不是一个特殊的函数(例如与 C 不同)。
您需要main()在脚本中显式调用。
一个常见的习惯用法是仅当您的文件作为脚本运行(而不是导入)时才这样做:
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
请参阅文档了解其工作原理