rendered paste body# Bilkostnad# coding: utf-8def openFile(fileName, mode): ''' Försöker öppna fil ''' try: theFile = open(fileName, mode) except: print 'Det gick inte att öppna filen, avslutar programmet.\n' sys.exit() else: return theFiledef nextLine(theFile): ''' Returnerar nästa rad från filen bilar ''' line = theFile.readline() line = line.replace("\n", "") return linedef nextBlock(theFile): ''' Returnerar nästa block, eller nästa "bil" och dess egenskaper ''' carmodel = str(nextLine(theFile)) if carmodel != "": price = int(nextLine(theFile)) gasconsumption = float(nextLine(theFile)) service = int(nextLine(theFile)) return carmodel, price, gasconsumption, service else: return False, False, False, Falsedef main(): use = int(raw_input('Hur mycket kommer bilen användas? (Km / år): ')) years = int(raw_input('Hur länge kommer den användas? (år): ')) gasPrice = float(raw_input('Vad är bränslepriset? (kr / liter): ')) carsFile = openFile("bilar.txt", 'r') carmodel, price, gasconsumption, service = nextBlock(carsFile) while carmodel: print carmodel print 'Totala kostnaden för en', carmodel, 'är: ', price + (use * gasPrice * gasconsumption) + (years * service) carmodel, price, gasconsumption, service = nextBlock(carsFile) carsFile.close() main()