1

I have been trying to convert my code from java to python and I come across this issue. I am trying to make a calendar and everything I input the month and year, I come across this issue that I do not know what is means.

May you guys help me out?

Traceback (most recent call last):
  File "/Users/macbook/Documents/Untitled.py", line 41, in <module>
    main()
  File "/Users/macbook/Documents/Untitled.py", line 30, in main
    m, y = eval(input("Enter the month, and year (separated by spaces): "))
  File "<string>", line 1
    12 2013
          ^
SyntaxError: unexpected EOF while parsing

My code:

#------------------------------------------------------------------------
def isLeapYear():
    if((year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)):
        return true
    else:
        return false

#---------------------------------------------------------------------
def dayOfWeek(mon, day, year):
    if(mon < 3):
        year = year-1

    wy = 3 + year + (year/4) - (year/100) + (year/400)
    wm = int(2.6 * ((mon+9 % 12) +0.5))
    wd = day-1
    return (wd + wm + wy) % 7

#---------------------------------------------------------------------
def daysInMonth(month, year):
    if(month ==4 or month ==6 or month ==9 or month==11):
        return 30
    elif(month==2):
        if(isLeapYear(year)):
            return 29
        else:
            return 28
    else:
        return 31

def main():
    m, y = eval(input("Enter the month, and year (separated by spaces): "))
    print("Sun Mon Tue Wed Thu Fri Sat\n")
    i=0
    while(i<dayOfWeek(m,1,y)):
        print("     ")
        i=i+1

    d=1
    while(d <= daysInMonth(m,y)):
        print(d)
        if(dayOfWeek(m,d,y) == 6):
            print("\n")
        d=d+1

main()

2 Answers 2

2

eval() only takes valid Python expressions. 12 2013 is not a valid Python expression.

Either require that the numbers are separated by a comma ('12, 2013') or use a different method of parsing the date input.

Try to avoid eval() for parsing input; a user with less friendly intent could enter arbitrary code to mess up your program and hijack the process when you do that.

The following line would work just as well for your purposes:

m, y = map(int, input("Enter the month, and year (separated by spaces): ").split())
Sign up to request clarification or add additional context in comments.

4 Comments

thanks! however, after i fixed that issue my dates won't print out when i compile my program. i can't seem to figure out the problem.
@user3002936: your dayOfWeek(m,1,y) call never varies, so there is an infinite loop there.
oh okay, and i fixed that but it still doesn't print like a calendar should do. how do i print System.out.printf("%-5d", d) in python? im getting stuck there.
You can use str.format() formatting (new style): print(format(d, '-5d')).
1

The error is obviously here:

m, y = eval(input("Enter the month, and year (separated by spaces): "))

You seems to have entered the string 12 2013 and you ask Python to eval it. But, 12 2013 means nothings in Python (it's just two integers separeted by a space). You can

  • Add a comma between number, 12, 2013 and it'll worked with the same code
  • Ask numbers with two call of input

For security matters, you should implement the second solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.