Discussion:
[Tutor] syntax error
Sarah
2015-09-14 17:20:55 UTC
Permalink
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
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Joel Goldstick
2015-09-14 18:25:08 UTC
Permalink
Post by Sarah
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
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
--
Joel Goldstick
http://joelgoldstick.com
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-09-14 18:45:36 UTC
Permalink
Post by Sarah
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
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-09-15 09:00:06 UTC
Permalink
Hello,
print('cost:$',gross_cost)
print('cost: ${:.2f}'.format(gross_cost))
does do and why one would use this over my plain version?
gross_cost = 4.6
print('cost: $',gross_cost)
cost: $4.6
print('cost: ${:.2f}'.format(gross_cost))
cost: $4.60

So the .2f forces the output to be formatted as a floating
point number with 2 digits after the decimal point.
And the format() inserts the values into the {} markers
quantity = 4
unit_cost = 4
tax = 0.1
print('''I bought {} items at ${:.2f} with {}% tax,
... making a total cost of: ${:.2f}
... '''.format(quantity,
... unit_cost,
... int(tax*100),
... quantity * unit_cost * (1+tax)))
I bought 4 items at $4.00 with 10% tax
making a total cost of: $17.60

Notice this time that :.2f forced the integer unit_cost(4)
to be shown as a float with 2 decimal places(4.00).

There are lots of other codes you can use to modify the
formatting.

Check the language reference in the docs under 'formatting':

https://docs.python.org/3/library/string.html#formatstrings
--
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-09-14 18:29:06 UTC
Permalink
Post by Sarah
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
The above should be fine when saved and run from a .py file, but at the
interactive interpreter, you need to leave a blank line after functions.

Try leaving a blank after the print(...) line and before main().
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...