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 8996213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:43:28+00:00 2026-06-15T23:43:28+00:00

I’m in an introductory Python course. I’m taking a practice final and I don’t

  • 0

I’m in an introductory Python course. I’m taking a practice final and I don’t know exactly what to do for a question. It asks:

Complete the following method that places odd numbers found in the string s in consecutive elements of the list x.

def assign(s):
    length = _____
    x = _____*[0]
    index = 0
    for j in range(_____):
        digit = int(s[j])  #convert to int digit
        if _____: #test for odd number
            x[_____] = digit
            __________
    print("The odd numbers are")
    for j in range(index):
        print(x[j])

I don’t know if it’s the way it’s worded but I have no clue how to go about answering this. My best guess would be:

def assign(s):
    length = len(s)
    x = length*[0]
    index = 0
    for j in range(s):
        digit = int(s[j])  #convert to int digit
        if _____: #test for odd number
            x[_____] = digit
            __________
    print("The odd numbers are")
    for j in range(index):
        print(x[j])

I have no clue how to continue, or if I’m even starting it off right. I can’t change anything that’s already there, I can only fill in the blanks, and they all have to be filled in. Anyone know how to do this?

  • 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-06-15T23:43:28+00:00Added an answer on June 15, 2026 at 11:43 pm

    Here is the solution:

    def assign(s):
        length = len(s)
        x = length*[0]
        index = 0
        for j in range(length):
            digit = int(s[j])  #convert to int digit
            if digit % 2: #test for odd number
                x[index] = digit
                index += 1
    
        print("The odd numbers are")
        for j in range(index):
            print(x[j])
    

    You got the beginning right. An key point is that variable index points to the next odd number that should be added to list x.


    Note, however, that the template that was provided to you is horrible: it completely goes against the spirit of Python. It looks like C code forced into Python; this is not good practice, as Python offers much more powerful data structures than C. Here is a Pythonic version:

    def assign(input_string):
        odd_digits = []
        for char in input_string:
            digit = int(char)  # Convert to integer
            if digit % 2:  # If the digit is odd
                odd_digits.append(digit)
        print("The odd digits are", *odd_digits, sep='\n')  # or: ("The odd digits are", odd_digits)
    

    The main difference is that you have fewer variables: the code is more legible, one has to keep fewer variables in mind when reading the code. The variables that have disappeared are essentially indexes: they are often not needed (indexes are important in C because C is “close to the metal”: it is close to machine language, which requires indexes). As a consequence, the need for keeping track of the length of the input string disappears.

    You also have less cruft: in the original version, list x contained many 0 that were essentially useless (slower code, which is also harder to make sense of). Essentially, Python list x was forced to be like a C array: the variable number of odd digits in the string call for a list (variable size), not for an array (fixed size).

    Also, the fact that the code is shorter makes is faster to read, and to write!


    As suggested by Jon, an even more Pythonic version consists in using list comprehensions:

    def assign(input_string):
        odd_digits = [char for char in input_string if int(char) % 2]  # or: if char in '13579'
        print("The odd digits are", *odd_digits, sep='\n')  # or: ("The odd digits are", odd_digits)
    

    This is a good example of why Python code is so much faster to write and read than C code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have much knowledge about the IPv6 protocol, so sorry if the question
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y’all

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.