Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有输入的Python程序EOF错误_Python - Fatal编程技术网

带有输入的Python程序EOF错误

带有输入的Python程序EOF错误,python,Python,我一直在尝试将我的代码从java转换为python,我遇到了这个问题。我试图做一个日历和我输入的所有月份和年份,我遇到了这个问题,我不知道这是什么意思 你们能帮帮我吗 Traceback (most recent call last): File "/Users/macbook/Documents/Untitled.py", line 41, in <module> main() File "/Users/macbook/Documents/Untitled.py",

我一直在尝试将我的代码从java转换为python,我遇到了这个问题。我试图做一个日历和我输入的所有月份和年份,我遇到了这个问题,我不知道这是什么意思

你们能帮帮我吗

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
回溯(最近一次呼叫最后一次):
文件“/Users/macbook/Documents/Untitled.py”,第41行,在
main()
文件“/Users/macbook/Documents/Untitled.py”,主文件第30行
m、 y=eval(输入(“输入月份和年份(用空格分隔):”)
文件“”,第1行
12 2013
^
SyntaxError:分析时出现意外的EOF
我的代码:

#------------------------------------------------------------------------
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()
#------------------------------------------------------------------------
def isLeapYear():
如果((第%4年==0)和(第%100年!=0)或(第%400年==0)):
返回真值
其他:
返回错误
#---------------------------------------------------------------------
def dayOfWeek(周一、日、年):
如果(星期一<3):
年份=第1年
wy=3+年+(年/4)-(年/100)+(年/400)
wm=int(2.6*((周一+9%12)+0.5))
wd=第1天
返回(wd+wm+wy)%7
#---------------------------------------------------------------------
def daysInMonth(月,年):
如果(月==4或月==6或月==9或月==11):
返回30
elif(月=2):
如果(isLeapYear(year)):
返回29
其他:
返回28
其他:
返回31
def main():
m、 y=eval(输入(“输入月份和年份(用空格分隔):”)
打印(“星期一星期二星期三星期四星期五星期六”)
i=0
而(i
eval()
只接受有效的Python表达式。
12 2013
不是有效的Python表达式

要求数字用逗号分隔(
'121013'
),或者使用不同的方法解析日期输入

在解析输入时尽量避免使用
eval()
;不太友好的用户可能会输入任意代码来扰乱程序,并在您这样做时劫持进程

以下行也适用于您的目的:

m, y = map(int, input("Enter the month, and year (separated by spaces): ").split())

错误显然在这里:

m, y = eval(input("Enter the month, and year (separated by spaces): "))
您似乎输入了字符串
12 2013
,并要求Python对其求值。但是,
12 2013
在Python中没有任何意义(它只是两个被空格分隔的整数)。您可以

  • 在数字,
    121013
    之间添加一个逗号,它将使用相同的代码
  • 通过两次呼叫
    输入询问号码

对于安全问题,您应该实施第二种解决方案。

谢谢!但是,在我解决了这个问题之后,在编译程序时,我的日期不会打印出来。我似乎无法解决问题。@user3002936:your
dayOfWeek(m,1,y)
调用永远不会改变,所以那里有一个无限循环。哦,好吧,我已经解决了这个问题,但它仍然不能像日历那样打印。如何在python中打印System.out.printf(“%-5d”,d)?我被卡住了。你可以使用(新样式):
打印(格式(d),-5d'))