Skip to content
Advertisement

How to reverse an int in python?

I’m creating a python script which prints out the whole song of ’99 bottles of beer’, but reversed. The only thing I cannot reverse is the numbers, being integers, not strings.

This is my full script,

JavaScript

I understand my reverse function takes a string as an argument, however I do not know how to take in an integer, or , how to reverse the integer later on in the script.

Advertisement

Answer

You are approaching this in quite an odd way. You already have a reversing function, so why not make line just build the line the normal way around?

JavaScript

Which runs like:

JavaScript

Then pass the result to reverse:

JavaScript

This makes it much easier to test each part of the code separately and see what’s going on when you put it all together.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement