nom1 / exercise

various exercises in python
0 stars 0 forks source link

replace string concatenation (joining strings with +), with '.format' function #3

Closed pete911 closed 8 years ago

pete911 commented 8 years ago

strings have 'in built' function format. Format function takes variable number (any number) of arguments and replaces '{x}' (where x is index of the argument) with the supplied argument.

* Example: * "hello {0}, are you OK {0}?".format("Naomi") Here you supplied one argument to the format function you call on string. Run it and see what it does.

String can be saved in a veriable as well:

f = "today is {1}, {0} o'clock"
f.format("10", "Friday")

This will display today is Friday, 10 o'clock, because we use {1} - index one is second argument and then {0}.

nom1 commented 8 years ago

I don't understand specifically what you want me to do

pete911 commented 8 years ago

replace all print function where you use "+" with "".format() so it is easier to read (looks like you have only 2 occurrences).

nom1 commented 8 years ago

plus I've tried it, and it doesn't work using pycharm

pete911 commented 8 years ago

file 19_bmi.py, line 12. Replace 'print("your BMI is ",BMI)' with 'print("your BMI is {0}.".format(BMI))'

there's another occurrence in the other file, replace that one as well.

nom1 commented 8 years ago

ok

nom1 commented 8 years ago

'print("your BMI is {0}.".format(BMI))' done!