Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6790349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:42:53+00:00 2026-05-26T17:42:53+00:00

I made a function with a dictionary. The purpose of the function is to

  • 0

I made a function with a dictionary. The purpose of the function is to separate the input string into sets of 3 . If the input string value is not a multiple of 3, I want to delete the remainder [1 or 2]

my function was working perfectly until I added the part for deleting the remainders

def func(fx):
    d={'AAA':1,'BBB':2,'CCC':3}
    length=len(fx)
    if length % 3 == 0:
        return fx
    if length % 3 == 1:
        return fx[:-1]
    if length % 3 == 2:
        return fx[:-2]
    Fx=fx.upper()
    Fx3=[Fx[i:i+3] for i in range(0,len(Fx),3)]
    translate=[d[x] for x in Fx3]
    return translate


x='aaabbbcc'

output = func(x)

print output

>>>
aaabbb

the function is recognizing that the input sequence is not a multiple of 3 so its deleting the 2 values which is what i want. However, its splitting the new string into 3 letter words to be translated with my dictionary anymore. If you delete the if statements, the function works but only for strings that are a multiple of 3.

What am I doing wrong ???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:42:54+00:00Added an answer on May 26, 2026 at 5:42 pm

    You are returning fx when you probably should be reassigning it

    def func(fx):
        d={'AAA':1,'BBB':2,'CCC':3}
        length=len(fx)
        if length % 3 == 0:
            pass
        elif length % 3 == 1:
            fx = fx[:-1]
        elif length % 3 == 2:
            fx = fx[:-2]
        Fx=fx.upper()
        Fx3=[Fx[i:i+3] for i in range(0,len(Fx),3)]
        translate=[d[x] for x in Fx3]
        return translate
    

    Here is an alternate function for you to figure out when you know some more Python

    def func(fx):
        d = {'AAA':1,'BBB':2,'CCC':3}
        return [d["".join(x).upper()] for x in zip(*[iter(fx)]*3)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone got a ready made function that will take an XML string and return
function randomString( len ) { // A random string of length 'len' made up
I made a function which will look up ages in a Dictionary and show
I want to make my Java Application to call my own custom made function
I made a function to truncate a string in the code behind file. But
I made a function which displays date on the webpage,, and i uploaded the
I made this function to verify a user's twitter credentials. Its running on two
I've made some function that generates an email template. Code it generates is pure
Hi have made this function which is made to replicate an error that I
I have made a function to cound the weeks in a year, and that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.