Lists in files into printing the cheapest price. Python -
i have -
showfile = open("products.txt", 'r') lines = showfile.readline() while lines: print(lines) lines = showfile.readline() showfile.close() showfile = open("products.txt", 'r') numbers = [] line in showfile: tokens = line.split(',') numbers.append(min(t t in tokens[0])) minvalue = min(numbers) print(minvalue)
products.txt -
'snicker's, 33, 84 'mars', 18.5, 72 'bounty', 22, 96
so 33 price, , 84 quantity. , 18.5 price , 72 quantity , on.
im trying make prints like- snickers $0.39 per unit. mars $0.29 per unit. bounty $0.23 per unit. bounty cheapest
help appreciated :d
you can use print repr(tokens)
show what's in tokens
variable. suggest add , see says.
note python has different types of values. e.g. "18.5"
or '18.5'
strings - these appropriate things strings (e.g. 'bounty'
) they're not numbers because can't maths on them.
if have number, want convert float
form (e.g. 18.5
) or int
form (e.g. 18
). python has functions that, called float()
, int()
. can normal maths (+-*/
) on floats , ints.
(in case above isn't clear: repr
print strings quotes around them; won't print quotes around floats or ints. repr
print decimal point floats, , never ints).
note float('18.5')
work, float(' 18.5')
won't, because of stray space. if have problem, strip()
function removes leading , trailing spaces string.
Comments
Post a Comment