Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Please check code
#1
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!!!
Quote
#2
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.
Quote
#3
(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?
Quote
#4
(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?
Quote
#5
(Apr-05-2019, 05:50 PM)nilamo Wrote:
(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?
and so?
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")

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  sending arduino data to python and use a parity check 10_Lucas 6 248 Aug-03-2019, 10:37 AM
Last Post: DeaD_EyE
  How to check an array exist in a file using Python fitrisibarani 17 749 Feb-27-2019, 04:16 PM
Last Post: ichabod801
  Hi how to take row 1 for every column and check the value what class is it kirito85 2 477 Nov-21-2018, 06:52 AM
Last Post: kirito85
  Check if string is uppercase or lowercase and eliminate Wolfpack2605 1 1,523 Jan-01-2018, 05:03 AM
Last Post: Mekire
  Check if integer is between two values Wolfpack2605 3 14,782 Dec-24-2017, 06:39 AM
Last Post: DeaD_EyE
  Iterating over pandas.df to check for values out of range Padowan 14 4,627 Nov-26-2017, 04:37 PM
Last Post: heiner55
  check conenction takes byte or not viens 1 798 Nov-01-2017, 06:45 PM
Last Post: nilamo
  "Call the subroutine in order to check..." misa_san 1 1,025 Sep-24-2017, 09:30 AM
Last Post: stranac
  How can I check out my localhost? Mike Ru 2 1,251 Aug-05-2017, 05:50 PM
Last Post: Mike Ru
  Ways to check if the variable works. AlwaysNew 3 1,495 May-04-2017, 05:51 AM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)