Python Forum
I have a logic error.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I have a logic error.
#1
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...

# 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.
Reply
#2
You are always comparing the same character: user_string[1]. You probably meant that to be user_string[i], which is one of the reasons non-descriptive variable names like 'i' are a bad idea. Also, in Python you want to be iterating directly over an object if possible, not using indexes to get at the piece you want:

for char in user_string:
    ordinal = ord(char)
    if 97 <= ordinal <= 122:
        ...
Note how I used ordinal. It's generally better to do a calculation once rather than multiple times. Doing it once leads to less buggy and more maintainable code. Also note how I chained the operators in the if statement. This is a common way of checking if a number is in a range.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
The string is probably utf-8. if you convert to ASCII before running through loop, I think you'll be OK.
You can see how to do this here: https://pymotw.com/3/codecs/
Reply
#4
(Sep-06-2017, 10:00 PM)ichabod801 Wrote: You are always comparing the same character: user_string[1]. You probably meant that to be user_string[i], which is one of the reasons non-descriptive variable names like 'i' are a bad idea. Also, in Python you want to be iterating directly over an object if possible, not using indexes to get at the piece you want:

for char in user_string:
    ordinal = ord(char)
    if 97 <= ordinal <= 122:
        ...
Note how I used ordinal. It's generally better to do a calculation once rather than multiple times. Doing it once leads to less buggy and more maintainable code. Also note how I chained the operators in the if statement. This is a common way of checking if a number is in a range.

Oh! Yeah, I did mean to have an "[i]" there. I'll keep everything you said in mind. Thanks for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework, works but has logic error goldielocks789 3 1,200 Apr-24-2023, 11:52 AM
Last Post: Larz60+
  Logic error - I need User_1 and User_2 to become true when both users sign in. BruhMoments 3 1,934 Jan-20-2021, 10:26 PM
Last Post: BruhMoments

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020