1. Engineering
  2. Computer Science
  3. write a python program that prompts the user for a...

Question: write a python program that prompts the user for a...

Question details

Write a Python program that prompts the user for a binary string, and then compute the length of the string without using any built-in python functions (e.g len() ). Also, compute the number of 1's in that binary string (polarity of a binary string).

You should first write a Python function polarity() which accepts one argument, a String that contains only the characters 0 or 1. This function should complete the following tasks:
• Calculate and the length of the String without using the len() function

• Compute the polarity of the String

• Return the length and polarity of the String

Notes: The polarity() function should not print any values – it should only calculate and return them.

Next, write a Python function named main() that accepts no arguments. The main() function should complete the following tasks:

• Prompt the user to enter a String containing only the characters 0 or 1

• Calls the polarity() function providing the String as an argument

• Assigns the two values returned by the polarity()function to variables

• Using the print() function, display the values returned by the polarity()function.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution