-1

I am a young student, and I'm wanting to learn coding. However, my if function is causing an issue and I'm not sure as to why. I'm sorry if anything is hard to understand or if my code is over-complicated; I'm not sure how to make it quicker.

As one of my first experiences with coding, I tried to create a basic bit of code, saying how old the user is in years, months, and days, based on their inputs. This had worked, however, if I input the month as 2, while the current month as 1, it would become a negative number. To try and stop this, I added an "if" function. This isn't working and I'm not sure as to why.

from time import localtime

year = localtime().tm_year
month = localtime().tm_mon
day = localtime().tm_mday

print("What day, month, and year were you born?")

day_input = int(input())
month_input = int(input())
year_input = int(input())

year_output = year - year_input
month_output = month - month_input
day_output = day - day_input

if month_input>month and month == "1" or month_input>month and month == "3" or month_input>month and month == "5" or month_input>month and month == "7" or month_input>month and month == "8" or month_input>month and month == "10" or month_input>month and month == "12":
    month_output = 12-month
    day_output = 31-1
    year_output = year-year_input-1

if month_input>month and month == "2":
    month_output = 12-month
    day_output = 28-1
    year_output = year-year_input-1

if month_input>month and month == "4" or month_input>month and month == "6" or month_input>month and month == "9" or month_input>month and month == "11":
    month_output = 12-month
    day_output = 30-1
    year_output = year-year_input-1

print("You are", year_output, "years,", month_output, "months, and", day_output, "days old!")
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
gooce
  • 1
  • 2
  • 3
    I'm guessing that `month` is an `int`, so you should be comparing against `int`s (e.g. `1`) instead of `str`s (e.g. `"1"`). – 0x5453 Jan 25 '23 at 20:54
  • 2
    Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jan 25 '23 at 20:55
  • Your conditions can also be greatly simplified. For example: `if month_input > month and month in {1, 3, 5, 7, 8, 10, 12}:` – 0x5453 Jan 25 '23 at 20:55
  • It also helps to break down your logic. String together a lot of `and`s and `or`s and how they interact can get unclear to readers. Using parenthesis to be explicit about precedence helps. – Charles Duffy Jan 25 '23 at 20:56
  • 2
    And then on the string side, rather than printing a sequence of values you can use f-strings to just put the data directly in the string: `f"You are {year_output} years, {month_output} months, ..."` – Mike 'Pomax' Kamermans Jan 25 '23 at 20:57
  • if the month delta is negative, you might take it and add 12 then substract 1 from the year delta – JonSG Jan 25 '23 at 20:58
  • 1
    Welcome to Stack Overflow. Please read [ask] and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and [mre], and carefully study code and look for problems before posting. In particular: if there is logic with `if` that is not doing what you expect, **check what the condition evaluates to**. It's complex condition and the result isn't what you expect? **check the individual parts**. Got it narrowed down to an individual part, such as `month == "1"`? **check the actual value, and its type**. Not what you expected? **Look back to see where it comes from**. – Karl Knechtel Jan 25 '23 at 20:59
  • 1
    But in any event, please note that this is **not a discussion forum**. Questions here **are not** so we can help you "learn coding" or get a project done; they're here to **build a library** of searchable questions. That means they need to be specific, clear, and focused, and (if applicable) pertain to short blocks of code that directly show the problem. – Karl Knechtel Jan 25 '23 at 21:01

2 Answers2

0

As the user 0x5453 had said, the variable comparation needed to be between int variables and not strings. So if you just change the if lines to:

if month_input>month and month == 1 or month_input>month and month == 3 or month_input>month and month == 5 or month_input>month and month == 7 or month_input>month and month == 8 or month_input>month and month == 10 or month_input>month and month == 12:

if month_input>month and month == 2:

if month_input>month and month == 4 or month_input>month and month == 6 or month_input>month and month == 9 or month_input>month and month == 11:

The calculation will be fine.

As others users had cited, your code isn't optimized, but, it isn't your question focus. I suggest follow the hints given.

amkawai
  • 353
  • 3
  • 10
  • 1
    The code is definitely not optimized: the condition `month_input>month` is present literally *twelve times*! It might be helpful to consider the condition `month_input>month` *just once*, and everything else inside the corresponding block... – printf Jan 25 '23 at 22:52
-3

hi the problem is that at the first, second and third lines the program thinks that localtime is a variable.

Tacchinus
  • 1
  • 1
  • 3
    Because the first line is `from time import localtime`, the `localtime` function _is_ available in the current namespace. There's nothing wrong with those lines, except insofar as they're calling `localtime()` three different times, potentially getting a different result each time if the calls happen on different sides of a date boundary. – Charles Duffy Jan 25 '23 at 21:03
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 27 '23 at 10:31