Can anyone help me with this problem?

Write a function named format_number that takes a non-negative number as its only parameter.

Your function should convert the number to a string and add commas as a thousands separator.

For example, calling format_number(1000000) should return "1,000,000".

Hello, @itsantoniobitch, and welcome to Python Software Foundation Discourse!

For this project, you may be interested in f-strings. See:

For an example, we can create a variable named num that represents a number, and display it as follows:

num = 7000000
print(f'{num:,}')

Output:

7,000,000

You can use that example to write your function. Please let us know how it works out.

The above code was EDITED to add an omitted parenthesis.

3 Likes
format_number = "{:,}".format

Try it online!

3 Likes

If I was grading a homework exercise to write a function that formats numbers with commas, I would give zero for a solution that merely calls a builtin function to do it.

I’m not even sure if I would allow solutions that call str() on the number.

I’m pretty sure that the ultimate purpose of the exercise is not actually to format a number string with commas, but to learn how to come up with algorithms and implement them in code.

What have you tried?

Do you know how to write a function?

Are you allowed to call existing builtin functions to:

  1. do the formatting for you?
  2. convert the number to string digits?

Now would be a good time for the original poster to let us know whether this is an assignment, and if so, what is allowed. In any case, it might be more creative for us to solve it without using functions to do the formatting for us.

So, let’s note the following:

>>> num = 1234567
>>> num // 1000
    1234
>>> num % 1000
    567

Some iteration combined with the above, and with some concatenation, would be very helpful. If the OP has learned about recursion, it presents some additional interesting possibilities.

divmod would be a another way of doing that, and you don’t need to use recursion.

1 Like

This is precisely why I hated school and university assignments, and why I personally would not want to become a professor or a teacher. It always felt like you must be hypernormalized into uncreative solutions to problems that teachers have inside their head, as opposed to clever, elegant and aesthetically pleasing ones. You come into a classroom expecting to reap the benefits of Western culture, and in the end the main takeaways are increased conventionalism and deference to authority, because stepping one centimeter outside their framework of unjustified implicit assumptions yields you a lower grade. In my opinion, @pochmann answer is literally perfect as it demonstrates a mastery of out-of-the-box thinking, and giving anything but 10/10 to it highlights the irrationality of Western educational system all the way down.

@pochmann’s solution is perfect if what you care about is formatting a number with commas. It’s a solved problem.

But it is useless if you care about learning how to code.

If you went to a cookery school, to learn how to cook, and the instructor gave you an assignment to cook a burger, and your solution was to order a burger from UberEats, well, that’s missing the point of school and learning.

If you just want to eat a burger, order one from somebody who has already solved the problem of cooking burgers. But if you want to learn how to cook, you actually have to cook.

There are two sorts of programmers:

  • those who can only use pre-packaged functions that somebody else wrote;
  • and those who can create those functions in the first place.

In another decade or so, the first group are going to be put out of work by AI. Welcome to the future :slight_smile:

None of what you have said is explicitly stipulated by teachers or professors 90% of the time. You are presented with a problem, solve a problem using out-of-the-box thinking, and then promptly hit with the “actually, the way you’ve done this teaches you nothing from my personal perspective” treatment.

That’s my problem with the burger analogy. In it, you were assigned an assignment to cook a burger and then paid somebody to cook it for you, which is effective cheating. Whereas coding assignments follow the structure of “you need to make X do Y”, but you may as well always add a clause of “but do it in the exact same way I expect you to do mutatis mutandis, lest I reduce your grade”. A better analogy to the burger would be if a teacher asked you to have a burger present at your table, but then negatively graded any person who did not cook a burger because post hoc you find out that he thinks that buying a burger gives you no pedagogical value. So you go to school to learn maths, but none of this works if you’re also not clever at decoding implicatures. Back in my middle school years, this pretty much killed any enjoyment I had for formal education.

If this is an assignment, it might be part of a unit on either integer arithmetic, iteration, or recursion. It could even be about something else. In any of these situations, students can be encouraged to use the assignment as an opportunity to work with the concepts at hand. Let them have fun with it. A focus on a particular technique need not preclude also giving them the opportunity to try out a variety of prepackaged solutions.

Let’s order the burger, enjoy it, then get creative with cooking some ourselves, or we can cook our burgers first, then order several and compare them to our own.

1 Like

I disagree. That code needs learning, too.

1 Like

Unfortunately, it appears that the OP may have left the room, so we might never find out what the object of the assignment was, or whether it was even a school assignment at all. It could simply be that the OP encountered this problem online while seeking a means of formatting numbers on a website dedicated to performing calculations related to currency. A little poking around for contextual information suggested that hypothesis to me.

In an educational unit focused on functions, methods, and binding to objects, that could be a nice example to study. Students learning about Python functions and methods might not realize, at first, that they are objects that can be assigned to variable names.

At the risk of overcooking the burger analogy …

>>> atoms_in_a_cheeseburger = 10000000000000000000000000
>>> format_number = "{:,}".format
>>> format_number(atoms_in_a_cheeseburger)
'10,000,000,000,000,000,000,000,000'

In the right context, there is a lesson in those lines of code.

Source of cheeseburger information:

EDITED above:

  • burger metaphor → burger analogy
  • lines of code reordered
1 Like