[Tutor] syntax error
Nym City
nymcity at yahoo.com
Tue Sep 15 03:41:49 CEST 2015
Hello,
I am also just trying to understand this code and could not understand the print statement.I would have just used:
print('cost:$',gross_cost)
Do you mind telling little bit about what:
print('cost: ${:.2f}'.format(gross_cost))
does do and why one would use this over my plain version?
Thank you.
On Monday, September 14, 2015 2:46 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
On 14/09/15 19:25, Joel Goldstick wrote:
> On Mon, Sep 14, 2015 at 1:20 PM, Sarah <soertly at gmail.com> wrote:
>
>> Hi
>> What's wrong with the following code?
>>
>> def main()
>> lunch = int(input('How many hours did you eat?'))
>> cost = float(input('Enter the hourly cost: '))
>> gross_cost = lunch * cost
>> print('cost:$', format(cost, '.2f'), sep='')
>> main()
>>
>>
>> I get the error File "<Stdin>", line 6
>>
>> Thanks, Sarah
>>
>
> You forgot the : after def main(...):
Also your print line is, I suspect, wrong.
I assume you want to print gross_cost not cost?
You could also use the format method of the string which
tends to be more flexible:
print('cost: ${:.2f}'.format(gross_cost))
hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list