Sep-06-2017, 09:48 PM
So for my homework assignment, I have to write a program that basically counts the characters and types of characters in a string that's entered by the user.
Here's the official description:
- Write a program that prompts for a sentence and calculates the number of
uppercase letters, lowercase letters, digits, and punctuation. Output the
results neatly formatted and labeled in columns.
- Do NOT use a "split()" function.
Here's the code I've come up with so far...
Any advice would be super helpful.
Thank you.
Here's the official description:
- Write a program that prompts for a sentence and calculates the number of
uppercase letters, lowercase letters, digits, and punctuation. Output the
results neatly formatted and labeled in columns.
- Do NOT use a "split()" function.
Here's the code I've come up with so far...
# get user input user_string = input("Enter a sentence: ") # create and initialize variables upper_case = 0 lower_case = 0 digits = 0 spaces = 0 punctuations = 0 x = len(user_string) #loop conditions and increment for i in range(0, x, 1): if(ord(user_string[1]) >= 97) and (ord(user_string[1]) <= 122): lower_case += 1 elif(ord(user_string[1]) >= 65) and (ord(user_string[1]) <= 90): upper_case += 1 elif(ord(user_string[1]) >= 48) and (ord(user_string[1]) <= 57): digits += 1 elif(ord(user_string[1]) == 32): spaces += 1 else: punctuations += 1 # print totals print(" Uppercase: " + str(upper_case)) print(" Lowercase: " + str(lower_case)) print(" Digits: " + str(digits)) print("Punctuations: " + str(punctuations))It basically doesn't count the correct types of characters. Say if I put in "Hello World!", It'd print out "Lowercase: 11" or something like that.
Any advice would be super helpful.
Thank you.