Reputation: 41
date = raw_input()
while date!="END" or date!="end":
day = date[0:2]
month = date[3:5]
monthsingle = date[3:5]
monthsingle =str(int(monthsingle))
monthsingle = int(monthsingle)
What I am trying to accomplish here is assign the month's number to monthsingle
to use it later in my code. The problem is the user is allowed to type "02" for February. How can I do this without this error:
monthsingle =str(int(monthsingle))
ValueError: invalid literal for int() with base 10: ''
Upvotes: 0
Views: 6383
Reputation: 59
Solution in Java
Implement a program to generate and display the next date of a given date. The date will be provided as day, month and year as shown in the below table. The output should be displayed in the format: day-month-year. Assumption: The input will always be a valid date.
class NextDate
{
public static void main(String[] args)
{
// Implement your code here
int day = 31,month = 12, year=15,monthLength;
year = 2000+year;
char leap;
if((year % 4==0) & (year % 100 == 0) || (year % 400 == 0))
{
leap = 'y';
}
else
{
leap = 'n';
}
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
monthLength = 31;
}
else if(month == 2)
{
if(leap == 'y')
{
monthLength = 28;
}
else
{
monthLength = 29;
}
}
else
{
monthLength = 30;
}
if(day < monthLength)
{
day++;
}
else if(month == 12 & day == monthLength)
{
day = 1;
month=1;
year++;
}
System.out.println(day+"-"+month+"-"+year);
}
}
Upvotes: -1
Reputation: 25
def generate_next_date(day,month,year):
#Start writing your code here
if((year%400==0 or year%4==0) and month==2):
next_day=day+1
next_month=month
next_year=year
elif(month==2 and day==28):
next_day=1
next_month=month+1
next_year=year
elif(month==12 and day==31):
next_day=1
next_month=1
next_year=year+1
elif(day==31 ):
next_day=1
next_month=month+1
next_year=year
elif((day==30) and (month==4 or month==6 or month==9 or month==11)):
next_day=1
next_month=month+1
next_year=year
else:
next_day=day+1
next_month=month
next_year=year
print(next_day,"-",next_month,"-",next_year)
generate_next_date(28,2,2015)
Upvotes: -1
Reputation: 414079
It is not a problem that the user is allowed to type 02 for February. The error message indicates that the issue is that monthsingle
is empty (''
). If you slice a string beyond its end; you get an empty string.
It means that the input is not in dd/mm
format.
To parse the date without using datetime.strptime()
function:
while True:
try:
day, month = map(int, raw_input("Enter date dd/mm: ").split('/'))
# validate day, month here...
except ValueError:
print 'invalid input, try again'
else:
break
# use day, month to get the next date...
# you could use datetime module to check your answer:
from datetime import date, timedelta
print(date(date.today().year, month, day) + timedelta(1))
Upvotes: 0
Reputation: 23064
If the user input is less than four characters long, date[3:5]
will be the empty string.
You could check that the input string is valid before trying to convert it to an integer, or catch the exception and give the user a helpful error message. Unexpected user input should not cause your program to crash.
while True:
print('Please enter a date in format "dd/mm" or "end".')
date = raw_input() # use input() if you use python 3
if date.lower() == 'end':
print('good bye')
break
try:
day = int(date[0:2])
month = int(date[3:5])
print('Day is %d and month is %d' % (day, month))
# Day and month are integers.
# You should check that it's a real date as well.
except ValueError:
# could not convert to integer
print('invalid input!')
Upvotes: 1
Reputation: 199
In this implementation you can trim '0' at the begining:
monthsingle = date[3:5].lstrip('0')
Or check date[3:5]: it seems like there is a dot.
Upvotes: 0