0

I've been working through a book called Exercises for Programmers as I am trying to learn Python.

https://pragprog.com/book/bhwb/exercises-for-programmers

At exercise 8 I ran into some trouble; I am meant to write code that prompts for how many people wanna order pizza, how many slices each and then it must calculate how many pizzas must be ordered (1 pizza = 8 slices) according to the total number of slices (persons * slices each) and the number of leftover slices (if there are any).

I think I've managed to suss it out except that once executed the method that calculates the number of leftover slices prints a weird string (it also contains a hex value?).

I've modulated everything into two files (trying to build good habits);

file 01:

main.py

import pizza as p

while True:

number_of_people = input("How many people are hungry for pizza? ")
number_of_slices_per_person = input("And how many slices per person? ")

try:
    val = int(number_of_people) or int(number_of_slices_per_person)

    print("\n" + number_of_people + " people have ordered " +
          str(p.number_of_pizzas(number_of_people, number_of_slices_per_person)) + " pizzas.")

    if int(number_of_slices_per_person) == 1:
        print("Each person would like just a " + number_of_slices_per_person + " slice of pizza.")
    else:
        print("Each person would like " + number_of_slices_per_person + " slices of pizza.")

    print("There are " + str(p.number_of_leftover_slices) + " pizza slices left over.")
    break
except ValueError:
    print("Please enter a valid number")

file 02:

pizza.py

def number_of_pizzas(p1, s1):
    """Calculates the number of pizzas according to the specified 
    requirements"""
    total_pizzas = 0
    total_slices = int(p1) * int(s1)
    for s2 in range(0, total_slices):
        if s2 % 8 == 0:
            total_pizzas = total_pizzas + 1
return total_pizzas


def number_of_leftover_slices(total_pizzas):
    """Calculate the number of leftover slices of pizza"""
    import main as m
    total_pizzas = total_pizzas
    leftover_slices = (int(total_pizzas) * 8) % 
        int(m.number_of_slices_per_person)
return leftover_slices

And this is the output I get;

'4 people have ordered 2 pizzas.'

'Each person would like 3 slices of pizza.'

'There are < function number_of_leftover_slices at 0x7f0a95e2c7b8 > pizza slices left over.'

My issue is the string that gets printed instead of the number I am expected to have calculated. Any thoughts on what I might have done wrong?

Thanks.

1
  • 1
    The problem is in your print statement, that prints "There are ..." you are not calling the function, instead just printing the function name. It should be something like: print("There are " + str(p.number_of_leftover_slices(pass_total_pizza_here)) That will get rid of the weird print statement. Commented Feb 7, 2018 at 19:35

1 Answer 1

1

You have two problems. Firstly, you need to indent the return line at the end of number_of_leftover_slices

Next, when you call print on an object, Python will try to use either the _repr_ or _str_ method of that object to get a string representation of that object. What you're seeing is the string representation of your number_of_leftover_slices function. What you want to print is actually the output of that function.

n = p.number_of_pizzas(number_of_people, number_of_slices_per_person)
remain = p.number_of_leftover_slices(n)
print(remain) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.