Ask your own question, for FREE!
Computer Science 18 Online
Aqual:

I am making a Python program but can't think of how to improve it. Can I get some help? https://repl.it/join/eawpxrht-aqual

QuestionCoveBot:

What are you trying to fix?

Aqual:

The age part and to add to it

QuestionCoveBot:

\(\color{#0cbb34}{\text{Originally Posted by}}\) @Aqual The age part and to add to it \(\color{#0cbb34}{\text{End of Quote}}\) Explain more.

Aqual:

I want it to be able to calculate someones exact age, their astronomical sign, and their generation.

Aqual:

but i would have to edit it every day. is there a way for me to make a way for it to get the current day

QuestionCoveBot:

Let me check.

QuestionCoveBot:

Aqual, what happened to Repl.it?

Aqual:

wdym

QuestionCoveBot:

It'snot working right.

QuestionCoveBot:

The lines and the questions won't work.

Aqual:

try again

InsatiableSuffering:

Ok so if you want to grab the current date, there is a module you can import to your program for it to work. Take a look at this screenshot.

1 attachment
InsatiableSuffering:

You just need to import the date module from the datetime library, and from there you need to assign a value to hold the current date. In doing so, it will always calculate the date based on your location.

Aqual:

Is there a way for me to calculate someones exact age

QuestionCoveBot:

We are still looking. I and @InsatiableSuffering are still working on it.

Aqual:

Ok, Thank you.

InsatiableSuffering:

So the way to go about it is pretty simple. We can still utilize the date module from the datetime library to calculate one's age precisely. For the short answer, I'll give you this screenshot. For the long answer, I'll go into detail as to the logic behind this.

1 attachment
InsatiableSuffering:

Long answer: So what we can do here is create a function to calculate the age of an individual. First, we grab the values of the year, month, and day that a person was born, then use those as parameters for our birth value. By sending this in as our parameter, we now know the date we are supposed to compare today with. Now we just grab the value of today with today = date.today() Now here's where the logic might start to fall apart. Looking at the age variable, you can see that it has some weird mathematical operations being performed on it. The first part is pretty basic. This simply means that we are taking the year difference by grabbing the year values of both the variable today and the birthDate argument into account. That is the most basic part of calculating the year. Now in order to take month and day into account, we have to find the values of both. By using (today.month, today.day) It returns an integer value in which can now be compared to another integer value. We use the parenthesis to give priority to those functions. Now by separating out each of the values for comparison, we are ready to check the values for comparison. In my case, since today is May 26th and the birthdate I set is February 5th, we know that May 26th is farther into the year of 2020 than Feburary 5th. So the question here is: Is May 26th less than (or equal to, as it should be) February 5th? No. So essentially the less than (or equal to) operand performs an if statement within the age variable. Why this is important is because is if the statement is false, then a value of 0 will take the place of ((today.month, today.day) < (birth.month, birth.day)). Likewise, if the statement is true, then it returns a value of 1, saying that the date today has not reached May 26th. So now with that out of the way, our age variable looks like the following: age = 2020 - 1969 - 0 Thus giving us our age of 51 in my example.

Aqual:

Thank you.

QuestionCoveBot:

It's a long response, but it does work.

Aqual:

now i have to figure out which version to restore

QuestionCoveBot:

Restore the older vesion.

QuestionCoveBot:

*Version

Aqual:

I think I fixed it now

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!