Alex A
Alex A

Reputation: 3

Python function returning a weird string

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.

Upvotes: 0

Views: 817

Answers (1)

Batman
Batman

Reputation: 8947

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) 

Upvotes: 1

Related Questions