Categories > Coding > Python >
Reverse a String in python
Posted
I'm writing a Python software that writes out the entire song '99 bottles of beer' in reverse. The only thing I can't reverse are the numbers, which are integers rather than strings.
This is my entire script,
def reverse(str):
return str[::-1]
def plural(word, b):
if b != 1:
return word + 's'
else:
return word
def line(b, ending):
print b or reverse('No more'), plural(reverse('bottle'), b), reverse(ending)
for i in range(99, 0, -1):
line(i, "of beer on the wall")
line(i, "of beer"
print reverse("Take one down, pass it around")
line(i-1, "of beer on the wall \n")
I understand that my reverse function accepts a text as an argument after reading this article, but I don't know how to take in an integer or how to reverse the number later in the script.
Thank you in Advance for your help
Replied
First of all, you have multiple syntax errors that prevent the code from even running in the first place. Additionally, in the plural function you'd want to put the 's' before the word variable otherwise it will return as 'elttobs' (reversed = 'sbottle') rather than 'selttob' (reversed = 'bottles').
Assuming you're trying to reverse the integers where 96 would be 69, all you have to do is in the 'line' function change the 'b' to 'reverse(str(b))'. This converts the integer into a string and then reverses it. The fixed code is below:
def reverse(str):
return str[::-1]
def plural(word, b):
if b != 1:
return 's' + word
else:
return word
def line(b, ending):
print(reverse(str(b)) or reverse('No more'), plural(reverse('bottle'), b), reverse(ending))
for i in range(99, 0, -1):
line(i, "of beer on the wall")
line(i, "of beer")
print(reverse("Take one down, pass it around"))
line(i-1, "of beer on the wall \n")
I also fixed the syntax errors for you.
Cancel
Post
we are dead
Replied
im convicned this guy is an advanced bot running on chat gpt gathering more nautral data.
Cancel
Post
https://media.discordapp.net/attachments/1010636204225601659/1012865624797610044/sKQybOLT.gif
Ty for rep: Swiney, Byoke, Lion, Locust, Waves, Weeb, Nickk, darkn, Atari, CubeFaces, Lux14, Rice, Delta, Syraxes, Aeon, Jordan, Pluto, and Hiroku!
P.S, I like cats better too!
Ty for rep: Swiney, Byoke, Lion, Locust, Waves, Weeb, Nickk, darkn, Atari, CubeFaces, Lux14, Rice, Delta, Syraxes, Aeon, Jordan, Pluto, and Hiroku!
P.S, I like cats better too!
Replied
Reversing a string is not hard.
string = "Hello, world!"
reversed_string = string[::-1]
print(reversed_string)
This returns "!dlrow ,olleH"
Cancel
Post
hecker dude ngl i hecked 5 ips in 1 second also luaU_loadbiglongjuicythingy(rL);
Replied
It is significant to remember that you might need to work with an attorney to guide you through the procedure. You can learn the law and bargain with your husband for a fair settlement with the aid of an attorney. An uncontested divorce in Virginia occurs when all of the issues in the divorce are agreed upon by both parties. This includes alimony, the division of assets and debts, child custody, and child support. Searching local attorneys who focus on family and divorce law is the best approach to discover one for Virginia uncontested divorce. Our knowledgeable attorneys are on hand to assist you.
Website: https://srislawyer.com/uncontested-divorce-lawyer-virginia/
Cancel
Post
There are additional procedures that must be followed if you are seeking for an uncontested divorce in Virginia with a child. Reach our team.
Users viewing this thread:
( Members: 0, Guests: 1, Total: 1 )
Cancel
Post