-3

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')
Barmar
  • 741,623
  • 53
  • 500
  • 612
g.georgecccc
  • 75
  • 1
  • 6
  • 2
    You can't use `self` outside a method. – Barmar Mar 30 '20 at 18:27
  • 1
    See https://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values for how to write `if program == "KM" or "km"` properly. – Barmar Mar 30 '20 at 18:28
  • can you fix it for me? how can i use it that make it work? – g.georgecccc Mar 30 '20 at 18:28
  • 1
    `if kilometri==int or float:` is not even close to the correct way to tell if the input is an integer or floatl – Barmar Mar 30 '20 at 18:29
  • What is `metri`? – Barmar Mar 30 '20 at 18:30
  • `self` is just a regular local variable inside your methods, like any local variable, you can't use it outside your function. – juanpa.arrivillaga Mar 30 '20 at 18:30
  • 1
    Why are you assigning attributes inside the `__repr__` method? That looks more like the `__init__` method. – Barmar Mar 30 '20 at 18:30
  • 1
    Both `__repr__` and `__str__` should return a string. – Barmar Mar 30 '20 at 18:31
  • 1
    And, on top of that all, you didn't even mention what is wrong or post an error. I suggest you start working error by error because right now there are many issues with this code stated in the comments above. See also https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response for how to take the user input correctly – Tomerikoo Mar 30 '20 at 18:39
  • the error is what they arleady said.`print(self.kilometer) NameError: name 'self' is not defined` – g.georgecccc Mar 30 '20 at 18:42

1 Answers1

0

If you want to call a self variable outside of Class, try like this:

variable = ClassName()
variable.kilometri