Python 解析时出现意外的 EOF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29301641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:21:44  来源:igfitidea点击:

Unexpected EOF while parsing

pythonpython-3.4

提问by Moath Wafeeq

Objectives: I need to read cost and discount rate and number of year and calculate the time adjusted cost and time adjusted benefits and cumulative for both.

目标:我需要阅读成本和折扣率以及年数,并计算两者的时间调整成本和时间调整收益以及累积。

I receive this error:

我收到此错误:

Traceback (most recent call last):
  File "D:\python\codetest\hw.py", line 3, in <module>
    cost = eval(input("Enter Development cost :"))
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

When I remove evalthe code works fine.

当我删除eval代码时工作正常。

 #import numpy as np

cost = eval(input("Enter Development cost :"))
discrate = eval(input("Enter discount rate :"))

#operation cost list
opcost = []
#benifits list
benifits = []
#dicount rate list


#dicount rate list
discount=[]
#time adjusted cost
TAC = []
#time adjusted benifits
TAB = []

CTAC=[]

year = eval(input("Enter number of year "))

for i in range (year):
    opcost.append(eval(input("Enter operation cost :")))

for i in range (year):
    benifits.append(eval(input("Enter benifit for this year :")))



for i in range (year):
    pvn = (1/pow(1+discrate,i))
    # print (pvn)
    discount.append(pvn)

for i in range (year):
    TAC.append(discount[i] * opcost[i])

#print(TAC[i])

for i in range(year):
    TAB.append(discount[i] * benifits[i]))


#CTAC = np.cumsum(TAC)

#for i in range (year):
#    print(CTAC[i])

采纳答案by Martijn Pieters

When you use eval(), Python tries to parse the string you pass to it as a Python expression. You passed in an empty string:

当您使用 时eval(),Python 会尝试解析您作为 Python 表达式传递给它的字符串。您传入了一个空字符串

>>> eval('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

Rather than use eval()you should use a specific converter; if your costs are floating point values then use float()instead:

eval()您应该使用特定的转换器而不是使用;如果您的成本是浮点值,请float()改用:

opcost.append(float(input("Enter operation cost :")))

This can still cause errors if the user just hits ENTERand you get another empty string:

如果用户刚刚点击ENTER并且您得到另一个空字符串,这仍然会导致错误:

>>> float('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: 

You can still handle that case by catching the exception. See Asking the user for input until they give a valid responsefor more details on how to best do this, including how to handle repeatedly asking until valid input is given.

您仍然可以通过捕获异常来处理这种情况。有关如何最好地执行此操作的更多详细信息,请参阅询问用户输入直到他们给出有效响应,包括如何处理重复询问直到给出有效输入。