import os def LabA(): no_of_shares=int(input(\"how many shares to be bought : $\
ID: 3710579 • Letter: I
Question
import os
def LabA():
no_of_shares=int(input("how many shares to be bought : $"))
pp_share=int(input("Price per share : $"))
broker_comm=int(input("percentage commission of broker for each transaction :"))
annual_return=int(input("Average annual returns as percentage :"))
total_amount = no_of_shares*pp_share
commission=total_amount*broker_comm/100
amount_stock=total_amount+commission
revenue_in_ten = total_amount*(1+annual_return*0.01)**10
print ("Amount paid for stock alone is : $", total_amount)
print ('Commission amount : $', commission)
print ("Total amount paid : $" ,amount_stock)
print ("Revenue in 10 years :", "%.2f" % revenue_in_ten)
def Menu():
print("A:Investment Projection B:Retirement Planing C:Morgatge D:College Fund E:Exit Enter your Choice::")
def collegefund():
print('Please enter the following')
p=float(input('Principle: '))
IR=float(input('Interest rate: '))
print('Hi,',name)
print('-----------------------------')
file=open('OptD.txt','r')
lst=[]
for line in file:
line=line.split()
lst.append(float(line))
total=0
year_total=[]
k=0
for i in range(len(lst)):
if (i+1)%12!=0:
total=total+lst[i]
else:
year_total.append(total)
total=0
k=k+12
year_total.append(sum(lst[k:]))
print('Yearly savings')
for val in year_total:
print(val)
def mort():
file=open("load.txt","r")
principal=float(file.readline())
intrest_rate=float(file.readline()) #intrest in Percentage
duration=float(file.readline()) #Duraction in year
file.close()
intrest_rate=(intrest_rate/100)/12 #calculate monthly intrest
duration=duration*12 #duration in Month from year
temp=(1+intrest_rate)**duration #calculate 1+r^n
monthly_payment=principal*(intrest_rate*temp)/(temp-1)
monthly_payment=round(monthly_payment,2)
write_file=open("out.txt","w")
write_file.writelines("Hello,{0}".format(name))
write_file.writelines(" _____________________________________________________________________________ ")
write_file.writelines("Principal: {0} Intrest Rate(yearly): {1} Loan Term(in years) {2}: Monthly Mortgage is: {3}"
.format(principal,intrest_rate*12*100,duration/12,monthly_payment))
write_file.writelines(" ----------------------------------------------------------------------------")
write_file.close()
name=input("Enter your Name:")
while(True):
print(" Hello {0} , Please Select the service: ".format(name))
Menu()
choice=input()
if(choice=='A' or choice=='a'):
LabA()
print("")
elif(choice in ('B','b')):
print(" Service Coming Soon")
elif(choice in ('c','C')):
mort()
file = open("out.txt", "r")
print(file.read())
print(" Would you like to go back to Menu(Y/N):")
choice2=input()
if(choice2 in ('n','N')):
print("Thank you! Have a Nice Day !")
break
elif(choice=='D' or choice=='d'):
collegefund()
print("")
break
elif(choice in ('e','E')):
print("Thank you!")
break
else:
print("Invalid Input, Please Enter Valid Choice:")
Explanation / Answer
ValueError: could not convert string to float:
>>>
In file string in which have white space " " so function cant convert empty string to float
in function - lst.append(float(line)) which string ,contains only float value maybe have whitespace float(...) will fail show error
You have 2 options, ignore that value and continue with the next, or use a default value like 0 or some other
use this method and modify your code
ignore it or use default value
for line in reader:
for line in file:
line=line.split()
lst.append(float(line))
except ValueError:
# Empty string
if line == ' ':
lst.append(0.0)
# or
pass # ignore
else:
raise
try to understand and modify your code
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.