DEV Community

Lisa Ghosn
Lisa Ghosn

Posted on • Updated on

Python in Practice

Here are some exercises that I was working on this week:

Bug 1

I am building some very high quality chairs and need exactly four nails for each chair. I've written a program to calculate how many nails I need to buy to build these chairs but something is wrong.

chairs = '15'
nails = 4
total_nails = chairs * nails
message = 'I need to buy {} nails'.format(total_nails)
print('You need to buy {} nails'.format(message))

When I run the program it tells me that I need to buy 15151515 nails.

How do I fix it?
To correct the code, I need to change the variable; chairs, from a string into an integer. In order for me to do that, I need to remove the apostrophes on the value 15.
once this is done, the program will run correctly and tell me that I need to buy 60 nails.

Bug 2

I'm trying to run this program, but I get an error.

my_name = Penelope
my_age = 29
message = 'My name is {} and I am {} years old'.format(my_name, my_age)
print(message)

How do I fix it?
To correct the code, I need to make Penelope a string:
my_name = 'Penelope'

python hasn't recognised the data type that the value Penelope is and isn't sure how to proceed. Once we specify that this is a string using '' it will run as instructed.


Program 3

I have a lot of boxes of eggs in my fridge and I want to calculate how many omelettes I can make. each box of eggs contains six eggs and I need four eggs for each omelette.

number_of_eggs_for_omlette = 4
number_of_eggs_in_carton = 6
number_of_egg_cartons = 9
total_eggs = number_of_egg_cartons * number_of_eggs_in_carton
omlettes = total_eggs // number_of_eggs_for_omlette
left_over_eggs = total_eggs % number_of_eggs_for_omlette
message1 = 'there are {} eggs in total, this means you can make {} omlettes with {} eggs left over '.format(total_eggs, omlettes, left_over_eggs)
print(message1.format(message1))
Enter fullscreen mode Exit fullscreen mode

There are 54 eggs in total, this means you can make 13 omlettes with 2 eggs left over


I also want write a program to tell me how many eggs I need to buy to make x amount of eggs. For example, I want to make 25 omlettes, how many eggs do I need to buy??

number_of_eggs_for_omlette = 4
number_of_desired_omlettes = 25
number_of_eggs_in_carton = 6
total_eggs_needed = number_of_eggs_for_omlette * number_of_desired_omlettes
egg_cartons = total_eggs_needed // number_of_eggs_in_carton
message2 = 'to make {} omlettes, you will need {} eggs. That means you need to buy {} cartons of eggs' .format(number_of_desired_omlettes, total_eggs_needed, egg_cartons)
print(message2.format(message2))
Enter fullscreen mode Exit fullscreen mode

To make 25 omlettes, you will need 100 eggs. That means you need to buy 16 cartons of eggs


These programmes will print the information that I need, depending on the values i'm inputting into it.

Top comments (4)

Collapse
 
sreno77 profile image
Scott Reno

First of all, you should switch to "f-strings" for printing. They're MUCH easier to work with. realpython.com/python-f-strings/

Collapse
 
lisag profile image
Lisa Ghosn

Ooo very cool. Thank you for sharing @sreno77
I may redo these using f-strings, it makes a lot of sense to condense the code

Collapse
 
mohammadparsajavidi profile image
mohammadparsa-javidi

Perfect 👍👍👌👌

Collapse
 
lisag profile image
Lisa Ghosn

it is now! I've incorporated f-strings 👌🏽