-2

print('enter your age')
age = input()

print('this is how many days you have been alive')

time.sleep(1)

print('input the first three letters of the month you were born')

jan = 31
feb = 59
mar = 90
apr = 120
may = 151
jun = 181
jul = 212
aug = 243
sep = 273
oct = 304
nov = 334
dec = 365

month = input()

print('now for the day you were born. put this in number form without any "th"s or "nd"s ')

date = input()

print('your total age is:')

time.sleep(1)

print((int(age) * 365) + int(date) + int(month))

the error i get is ValueError: invalid literal for int() for month

I'm a beginner so if you want me to provide more detail let me know, but i believe this is an obvious fix that i just havent thought of yet

when i try removing the int() it gives another error, something like "expected int, got str instead

bad_coder
  • 11,289
  • 20
  • 44
  • 72
mintymoes
  • 25
  • 4

2 Answers2

2

You're not using the month values anywhere, you're basically trying to convert a string to int and it raises an error. Here's a possible fix

month_data = {'jan': 31, 'feb': 91 ...} # this is a dictionary
month = input()
month_data.get(month, None)
if month is None:
    print('invalid month supplied')
else:
    # print age

References:

Ceres
  • 2,498
  • 1
  • 10
  • 28
0

The problem is, you accept the month string, but want to use the int value you declared for the particular variable. You can use eval to get the value of the particular months variable.

But I guess the more pythonic solution is that of @Ceres using dictionaries.

import time

print('enter your age')
age = input()

print('this is how many days you have been alive')

time.sleep(1)

print('input the first three letters of the month you were born')

jan = 31
feb = 59
mar = 90
apr = 120
may = 151
jun = 181
jul = 212
aug = 243
sep = 273
oct = 304
nov = 334
dec = 365

month = input()
month_val = eval(month)

print('now for the day you were born. put this in number form without any "th"s or "nd"s ')

date = input()

print('your total age is:')

time.sleep(1)

print((int(age) * 365) + int(date) + int(month_val))
schilli
  • 1,700
  • 1
  • 9
  • 17
  • thanks, this one makes more sense, even though ceres's is probably more efficient, thanks heaps – mintymoes Jun 05 '21 at 20:51
  • https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice , https://stackoverflow.com/questions/1933451/why-should-exec-and-eval-be-avoided – Thierry Lathuille Jun 05 '21 at 20:54