简体   繁体   中英

ValueError: invalid literal for int() with base 10: '26.02.2018'

So I was bored and decided to do a little bit of coding using Python (I'm a noob), when i got this error:

Blockquote ValueError: invalid literal for int() with base 10: '26.02.2018'

Here is the full script:

print("This is case sensitive!")

#Ask for the users name
name_1 = input("What is your name?")

#Ask for the users gender
gender = input("Are you a male, a female or other(If other, then what?)?")

#Ask for the users birthday
birthday = input("When were you born?(DD.MM.YYYY)")

#Ask for the users location
location = input("In what country do you live?")

import datetime
now = datetime.datetime.now()
today = now.strftime("%d.%m.%Y")

today = int(today)
birthday = int(birthday)

age = today - birthday

if gender == "male":
    print("Mr.", name_1)
    print(age)
    print (birthday )
    print("Male")
    if birthday == today:
        print("Happy Birthday,Mr.", name_1)
    else:
        ""

elif gender == "female":
    print ("female")

else:
    print (gender)
    print ("Part of the LGBT+ Community.")

This script is not complete.

You are trying to cast '26.02.2018' to an int , which is impossible (two decimal points).

Parse it to a DateTime object instead, using datetime.strptime :

from datetime import datetime as dt
# ...
birthday = dt.strptime(birthday, '%d.%m.%Y')

Then compare it with today as DateTime objects instead of converting either to int .

You can't cast '26.02.2018' (a date) into an integer. Python doesn't know what to do with the periods. You can do this instead

todayarray= today.split('.')

To get an array containing the day in the first entry, the month in the second entry, and the year in the third entry. You could then similarly do the same to birthday (because that is also going to cause the exact same error when you fix this and make it to that line), and then perform your subtraction upon the two arrays by iterating through them in parallel with a for loop. Then, you can convert waht you get back back into what you want by doing this (we will assume you called the result array resultarray)

result=resultarray[1]+"."+resultarray[2]+"."+resultarray[3]"

Edit: Erm, I'm sorry for being unclear, but when you split it the way I instructed. The values in the strings will be able to be casted into integers. That's how you will implement this solution.

Edit2: I'll just be as clear as I can and type out a full solution

print("This is case sensitive!")

#Ask for the users name
name_1 = input("What is your name?")

#Ask for the users gender
gender = input("Are you a male, a female or other(If other, then what?)?")

#Ask for the users birthday
birthday = input("When were you born?(DD.MM.YYYY)")

#Ask for the users location
location = input("In what country do you live?")

import datetime
now = datetime.datetime.now()
today = now.strftime("%d.%m.%Y")

today = today.split('.')
birthday = birthday.split('.')
for x in range(0,3):
    age[x]=today[x]-birthday[x]
age = age[0]+"."+age[1]+"."+age[2]

if gender == "male":
    print("Mr.", name_1)
    print(age)
    print (birthday )
    print("Male")
    if birthday == today:
        print("Happy Birthday,Mr.", name_1)
    else:
        ""

elif gender == "female":
    print ("female")

else:
    print (gender)
    print ("Part of the LGBT+ Community.")

This will, ofcourse save ages as the amount of days, months, and years they have been alive, separated by periods. If you want another format, it should be easy enough to implement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM