简体   繁体   English

为什么 self 没有定义并给我不同的结果?

[英]Why does self is not defined and give me different results?

this code should do这段代码应该做

  • kilometer to meter convert公里到米换算
  • meter to kilometer convert米到公里换算
  • float should work浮动应该工作
  • should use classes and input应该使用类和输入
  • doesn't matter you input km or m it will convert each other to each other不管你输入公里还是米,它都会相互转换
  • sorry to my english对不起我的英语
class Convertor:    
    def __repr__(self, kilometer,meter):
        self.kilometer = metri/1000 
        self.meter = kilometri*1000

    def __str__(self):
        return self.kilometer, self.meter


program=input("please input number in KM or M: ")
if program=="KM" or "km":
    kilometri=input("please input only km: ")
if kilometri==int or float:
    print(self.kilometer)
else:
    print('please input only digits')

If you want to call a self variable outside of Class, try like this:如果你想在 Class 之外调用一个 self 变量,试试这样:

variable = ClassName()
variable.kilometri

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么myHDL手册中的示例为我提供了不同的结果? - Why does this example from the myHDL manual give me different results? 为什么 re.findall() 给我的结果与 Python 中的 re.finditer() 不同? - Why does re.findall() give me different results than re.finditer() in Python? 为什么在 python 和 php 上使用加盐的 hash 会给我不同的结果? - Why does using salted hash on python and php give me different results? 为什么函数参数之外的“self”会给出“未定义”的错误? - Why does “self” outside a function's parameters give a “not defined” error? 为什么列表理解会给我项目未定义错误? - Why does list comprehension give me item not defined error? 为什么 Parse_Dates 在 Python 中给我错误的结果? - Why does Parse_Dates give me the wrong results in Python? 为什么用php编写的河豚代码在Python中给出不同的结果? - Why does blowfish code written in php give different results in Python? 为什么 Scipy 和 EXCEL 中的 Bessel function 给出不同的结果? - Why does Bessel function in Scipy and EXCEL give different results? 为什么这会给我IndexError? - Why does this give me an IndexError? 为什么corr()给我的结果只有int,uint或float类型而不是对象类型? - Why does corr() give me results with only int, uint or float type and not with object type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM