0

I am working on a tax calculator in Python, and am wondering if it is at all possible to direct string-based input to a function. When I run the program, it does nothing past the initial input. My code snippet is as follows:

class TaxCalculator():
type_of_pay = input(
    "Do you get paid hourly or salary? (Enter hourly or salary)")
if type_of_pay == "hourly":
    
    def hourly(self):
        rate = input("What do you get paid per hour")
        hours = input("Roughly how many hours do you work per week?")
        overtime = input(
            "Do you earn overtime? If so, answer with the rate, otherwise press enter.")

I am not sure if I need to include the self bit in there, as I don't use it (still learning python).

  • 2
    As written, that's not even valid Python and won't run at all. If you are learning Python, maybe you should use a textbook example first. Of course it's possible to pass user input to a function. But explaining that to you with all the mistakes and misunderstandings would be too much – Thomas Weller Jan 28 '21 at 07:53
  • 1
    Welcome to stackoverflow. As Thomas mentions I think to you would have a benefit of going thorugh a standard python tutorial. But I would suggest implimenting this with either a function or just a plain script and not right away with a class. You can consider using classes later. By the way don't get discouraged by the downvote. – Peter Mølgaard Pallesen Jan 28 '21 at 07:58
  • Even if the code will be valid, you only create a function; but you are not using that function. check this one out: https://stackoverflow.com/questions/2573135/python-progression-path-from-apprentice-to-guru – r.burak Jan 28 '21 at 08:05
  • @r.b.leon: I had a quick glimpse at the first 10 answers there and I doubt they would be useful for improving OPs understanding of object orientation – Thomas Weller Jan 28 '21 at 08:10
  • @ThomasWeller do you have any suggestions? Not for just op. I would also appreciate it if you can suggest a path? – r.burak Jan 28 '21 at 08:12
  • 1
    Learn structural programming, then learn object oriented programming. This will help you with many programming languages, which is a good thing. After that, dive into Python specifics like mentioned in the linked answer. – Thomas Weller Jan 28 '21 at 08:18
  • I would recommend that first of all, don't try use a class for now, until you have an idea of how classes work. Also, try and get a working script with default values first instead of getting input (it will take a long time to test if you have to write input every time). Then you can try with the input lines, but after that you should try using command line arguments instead of `input()`. Things like asking the user for lines and lines of input take a long time and there's a lot of room for error. Note that `input()` returns a string, so if the user types `4` for `rate`, then `rate = '4'` – Rolv Apneseth Jan 28 '21 at 11:56

0 Answers0