1

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 eval the code works fine.

 #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])
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Moath Wafeeq
  • 45
  • 1
  • 3
  • 12

1 Answers1

2

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('')
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:

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

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

>>> 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 response for more details on how to best do this, including how to handle repeatedly asking until valid input is given.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343