Please check code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Please check code (/thread-17301.html) |
Please check code - Evgeniy2019 - Apr-05-2019 The program below implements computer assistant, who asks the user day, month and year of birth, and then display the age in years, months, and days. It would be a very cool program, but someone hacked our system and made changes to the code! In the if statement ** characters appeared, and the variable no longer stores data! Correct all errors and inaccuracies and run the program. --- CODE # Step 1 start import datetime # Step 2 birth day = int(input("Day of birth: ")) birth month = int(input("Month of birth: ")) birth year = int(input("Year of birth: ")) day = int(datetime.date.today().day) month = int(datetime.date.today().month) year = int(datetime.date.today().year) # Step 3 if * > **: age = year - birth_year then: age = (year - birth_year) - 1 # Step 4 a = age b = abs(month - birth_month) c = abs(day - birth_day) print("Your age: {a} years, {b} months, {c} days")-----MY CODE PLEASE CHECK !!! import datetime # Step 2 birth_day = int(input("Day of birth: ")) birth_month = int(input("Month of birth: ")) birth_year = int(input("Year of birth: ")) day = int(datetime.date.today().day) month = int(datetime.date.today().month) year = int(datetime.date.today().year) # Step 3 if year > birth_year: age = year - birth_year else: age = (year - birth_year) - 1 # Step 4 a = age b = abs(month - birth_month) c = abs(day - birth_day) print(f"Your age: {a} years, {b} months, {c} days")Thanks!!! RE: Please check code - nilamo - Apr-05-2019 The else looks wrong to me, as it means anyone born this year is -1 years old. But it doesn't look like your assignment was to fix logic issues lol. RE: Please check code - Evgeniy2019 - Apr-05-2019 (Apr-05-2019, 04:24 PM)nilamo Wrote: The else looks wrong to me, as it means anyone born this year is -1 years old. But it doesn't look like your assignment was to fix logic issues lol.How to fix problems? RE: Please check code - nilamo - Apr-05-2019 (Apr-05-2019, 04:07 PM)Evgeniy2019 Wrote:else: age = (year - birth_year) - 1 Don't think about code, just think about logic. This code will only run if the birth year is less than, or equal to, the current year. Which means either someone born this year (or at some point in the future). What should that person's age be? RE: Please check code - Evgeniy2019 - Apr-05-2019 (Apr-05-2019, 05:50 PM)nilamo Wrote:and so?(Apr-05-2019, 04:07 PM)Evgeniy2019 Wrote:else: age = (year - birth_year) - 1 import datetime # Step 2 birth_day = int(input("Day of birth: ")) birth_month = int(input("Month of birth: ")) birth_year = int(input("Year of birth: ")) day = int(datetime.date.today().day) month = int(datetime.date.today().month) year = int(datetime.date.today().year) # Step 3 if birth_month < month: age = year - birth_year else: age = (year - birth_year) - 1 # Step 4 a = age b = (month - birth_month) c = (day - birth_day) print(f"Your age: {a} years, {b} months, {c} days") |