Question: Python Programming. ASCII, the American Standard Code for Information Interchange, is a 20th century character-encoding scheme used to represent text in computers. Originally based on
Python Programming.
ASCII, the American Standard Code for Information Interchange, is a 20th century character-encoding scheme used to represent text in computers. Originally based on the English alphabet, ASCII includes just 128 basic characters: numbers 0 to 9, lowercase letters a to z, uppercase letters A to Z, basic punctuation symbols, the space character, and a few other non-printing characters. This means that all special punctuation, symbols, and non-Western characters cannot be represented in ASCII.
Here is a Python string of all the printable ASCII characters. ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
Using the string above, write a program that prompts a user to enter a text. Compare this text to the ASCII string and print out the following information. Structure the program using a FOR-LOOP.
Do not use a while-loop.
The total number of characters (NOT including spaces) in the text
The total number of white spaces
The total number of ASCII characters in the text
The total number of non-ASCII characters in the text
A list of the non-ASCII characters in the text seperated by a comma
Here is a sample running of the program.
Enter a text to analyze: Lets go to the caf! Total number of characters: 21 Total number of white spaces: 4 Total number of ASCII characters: 19 Total number of non-ASCII characters: 2 The non-ASCII letters were: ,
I asked this question before and someone gave me the right answer, but they used
def main():
and
if __name__ == '__main__': main()
I really do not understand that so if you could not use whatever that is i would really appreciate it.
Here is the full code. Do not use def main().
def main(): string = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~' char_count = 0 white_space_count = 0 ascii_count = 0 non_ascii_count = 0 non_ascii_chars = [] text = input('Enter a text to analyze: ') for ch in text: char_count += 1 if ch.isspace(): white_space_count += 1 if ch in string: ascii_count += 1 else: non_ascii_chars.append(ch) non_ascii_count += 1 print() print('Total number of characters: ' + str(char_count)) print('Total number of white spaces: ' + str(white_space_count)) print('Total number of ASCII characters: ' + str(ascii_count)) print('Total number of non-ASCII characters: ' + str(non_ascii_count)) print('The non-ASCII letters were: ', end='') for i, ch in enumerate(non_ascii_chars): print(ch, end='') if i != len(non_ascii_chars) - 1: print(", ", end='') print() if __name__ == '__main__': main() Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
