6
\$\begingroup\$

Hiii... Can you please check the code and see whether I might have done a mistake ? Beginner's eyes can be misleading quite easily. I feel like there's something wrong in this solution. I can't figure out what. so please check the solution and let me know. keep in mind i'm native sinhala speaker.

Assume hot dogs come in packages of 10, and hot dog buns come in packages of 8. Write a program that calculates the number of packages of hot dogs and the number of packages of hot dog buns needed for a cookout, with the minimum amount of leftovers. The program should ask the user for the number of people attending the cookout and the number of hot dogs each person will be given. The program should display the following details:

  • The minimum number of packages of hot dogs required.
  • The minimum of hot dogs that will be left over.
  • The number of hot dog buns that will be left over.
# Declare hot dogs and hot dog buns
HOT_DOGS_PER_PACKAGE = 10
HOT_DOGS_BUNS_PER_PACKAGE = 8

# Get the number of attendees
attendees = int(input('Enter the number of guests: '))

# Number of hot dogs per person
hot_dogs_per_person = int(input('Hot dogs per person: '))

# Number of hot dogs required
required_hot_dogs = attendees * hot_dogs_per_person
packages_of_hot_dogs = required_hot_dogs / HOT_DOGS_PER_PACKAGE

# Number of hot dog buns required
packages_of_hot_dog_buns = required_hot_dogs / HOT_DOGS_BUNS_PER_PACKAGE

print(f"You require {packages_of_hot_dogs} hot dogs for the cookout.")
print(f"You require {packages_of_hot_dog_buns} buns for the cookout.")
# Number of left over hot dogs
remain_hotdogs =  required_hot_dogs % HOT_DOGS_PER_PACKAGE
if remain_hotdogs != 0:
    print(f'You have {remain_hotdogs} left over hot dogs')

remain_buns = required_hot_dogs % HOT_DOGS_BUNS_PER_PACKAGE
if remain_buns != 0:
    print(f'you have {remain_buns} left over hot dog buns. ')

Program output

Enter the number of guests: 25
Hot dogs per person: 5
You require 12.5 hot dogs for the cookout.
You require 15.625 buns for the cookout.
You have 5 left over hot dogs
you have 5 left over hot dog buns. 
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to CodeReview. If you're not sure about your program, one easy way to check is to give it small numbers where you know the answer. If there's 1 person eating 1 hotdog, then your solution would require 0.1 hotdogs and 0.125 buns, and would have left over 1 hotdog and 1 bun. Whereas you should be requiring 1 pack of each, and have leftover 9 and 7. \$\endgroup\$
    – Teepeemm
    Dec 9 '21 at 16:08
  • \$\begingroup\$ @Teepeemm Thank you for the tip. currently i'm trying to write a money converting program. after written long codes and not working them, i recently started doing that. \$\endgroup\$
    – Jims-Rc
    Dec 9 '21 at 17:52
2
\$\begingroup\$

I think the error is in your output.

You require 12.5 hot dogs for the cookout.
You require 15.625 buns for the cookout.

It should be:

You require 12.5 packs of hot dogs for the cookout.
You require 15.625 packs of buns for the cookout.

But in the real world, you should use math.ceil for it:

You require 13 packs of hot dogs for the cookout.
You require 16 packs of buns for the cookout.


import math

print(f"You require {math.ceil(packages_of_hot_dogs)} packs of hot dogs for the cookout.")
print(f"You require {math.ceil(packages_of_hot_dog_buns)} packs of buns for the cookout.")

Also your math with buns doesn't add up. But you can fix it very easily.

import math


HOT_DOGS_PER_PACKAGE = 10
HOT_DOGS_BUNS_PER_PACKAGE = 8

attendees = int(input('Enter the number of guests: '))

hot_dogs_per_person = int(input('Hot dogs per person: '))


required_hot_dogs = attendees * hot_dogs_per_person
packages_of_hot_dogs = required_hot_dogs / HOT_DOGS_PER_PACKAGE
packages_of_hot_dog_buns = required_hot_dogs / HOT_DOGS_BUNS_PER_PACKAGE

print(f"You require {math.ceil(packages_of_hot_dogs)} packs of hot dogs for the cookout.")
print(f"You require {math.ceil(packages_of_hot_dog_buns)} packs of buns for the cookout.")

remain_hotdogs = (math.ceil(packages_of_hot_dogs) * HOT_DOGS_PER_PACKAGE) - required_hot_dogs
if remain_hotdogs != 0:
    print(f'You have {remain_hotdogs} left over hot dogs')

remain_buns = (math.ceil(packages_of_hot_dog_buns) * HOT_DOGS_BUNS_PER_PACKAGE) - required_hot_dogs
if remain_buns != 0:
    print(f'You have {remain_buns} left over hot dog buns. ')

Also, if you name your variable required_hot_dogs, you don't really need to add a comment to it. Your names are more than self-explanatory, so try to keep it nice and clean.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.