0

I'm absolutely terrible at Python and my Computer Programming class ends in two days (thank god), but I am having the hardest time figuring out what is probably the easiest code ever.

The instructions to my assignment state, "Write a program which reads in text until a '!' is found. Use an array of integers subscripted by the letters 'A' through 'Z'."

From what i have done so far:

msg = input("What is your message? ")

msg = msg.upper()

int_array = [0] * 26

for alph in range (65, 91):
    char = chr(alph)
    print(char)

(int_array[char])

any help would be greatly appreciated! thanks!

EDIT: This is the full assignment:

Write a program which reads in text from the keyboard until a ('!') is found.

Using an array of integers subscripted by the letters 'A' through 'Z', count the number occurrences of each letter (regardless of whether it is upper or lower case). In a separate counter, also count the total number of "other" characters ('.', '?', ' ', '2', etc.).

Print out the count for each letter found. Also, print the count of the non-letter characters.

By inspecting the array, print out the count of the number of vowels, and the number of consonants.

Print out which letter was found the most times. (Note there may be more than one letter which has the maximum count attached to it.) Print out which letter (or letters) was found the least number of times, but make certain to exclude letters which were not found at all.

UPDATE:

I have gotten this far with my code

msg = input("What is your message? ")

print ()

num_alpha = 26
int_array = [0] * num_alpha

for alpha in range(num_alpha):
    int_array[alpha] = chr(alpha + 65)
    print(int_array[alpha], end = "")

print()

lett = 0
otherch = 0
num_vowels = 0
num_consanants = 0

count_character = [0] * 100000

length = len(msg)

for character in msg.upper():
    if character == "!":
        print("lett =", lett)
        print("other char = ", otherch)
        print("num_vowels = ", num_vowels)
        print("num_consanants = ", num_consanants)
    elif character < "A" or letter > "Z":
        otherch = otherch + 1
        count_character[ord(character)] = count_character[ord(character)] + 1
    else:
        lett = lett + 1
        count_character[ord(character)] = count_character[ord(character)] + 1

for character in msg:
    print("character", character, "appeared" , count_character[ord(character)] , "times")

it's obviously not finished yet, but every time i print the last print statement, it says that each character appeared 0 times. can anybody help?

10
  • 4
    ...if it helps, I think that this assignment is utter bollocks. Why use an int array for characters, when a dict is much more natural? (why use an array at all? you're just reading text until ! is found...) Commented Aug 14, 2013 at 4:07
  • Are you supposed to do something with the text you read? It sounds like a part of the assignment description is missing here. Commented Aug 14, 2013 at 4:09
  • it does go on on further if it makes it any more reasonable? (number of occurences of each letter, total of "other" characters, count for each letter, number of consonants, number of vowels, etc...) Commented Aug 14, 2013 at 4:10
  • 2
    Agreed that the assignment is terrible! 1. Python doesn't have arrays: they are called lists, so the assignment writer is confused or doesn't know Python. 2. Python lists are only subscripted with ints; using "letters" as subsscripts (actually strings) probably means you need to use dicts. 3. What does having an "array" indexed from 'A' to 'Z' have to do with looking for exclamation marks? Commented Aug 14, 2013 at 4:11
  • 2
    @RayToal: I'd wager this is what happened: The class used to be in C. Someone other than this professor decided it would be in Python. Now it's being taught "in Python" by a C programmer. Commented Aug 14, 2013 at 15:37

2 Answers 2

2

You're going to need to get clarification on this, because there's no such thing as "an array of integers subscripted by the letters 'A' through 'Z'" in Python.

Possible interpretations that I can think of:

  • It's supposed to be a dictionary rather than an array. Python dictionaries are similar to lists (the Python datatype that is roughly equivalent to "arrays" in other languages), but they can be subscripted by strings, whereas lists can be subscripted only by integers. This way, you can store an integer to be associated with each letter. This is how most Python programmers would generally do something like this.
  • You're supposed to use parallel lists. You can do this by making two lists of 26 elements each, one containing the letters 'A' through 'Z' and one containing integers. For each letter, you could then use the list.index method to find the index in the first list where that letter is, then look up that index in the second list. (In theory, you wouldn't really need the first list, since Python strings are like lists in that they can be subscripted with integers and support the index method. So you could use the string 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' instead of a list. Or you could use the ord function, which is the inverse of the chr function. But I don't know if you're supposed to use these features.)
Sign up to request clarification or add additional context in comments.

3 Comments

we've never used dictionaries in class before, do you mind giving me an example? with the parallel lists, would it look something like: intlist = [1 , 2 , 3 ...] alphalist = ['A', 'B', 'C', ..] list.index (0, 0) ?
The list of integers would start off as 26 zeroes; you would increment these as you go. The index method tells you where in the list something is; for instance, alphalist.index('A') would return 0, alphalist.index('C') would return 2, etc.
2

I'm not 100% sure the following is right because I agree with the others that the assignment description is wonky. It looks like a C-based homework assignment lazily ported to Python. That said:

  • In principle rather than hardcoding the bounds of the alphabet I'd go with ord('A') and ord('Z')+1, so that I can say something like alphabet = list(range(ord('A'), ord('Z')+1))

  • Renaming int_array to counter might make it more obvious what you need to do in your inner loop (keeping in mind that you're using the letters as your indices. Or rather, you'd need something more like ord(letter)-ord('A') as your indices)

  • You don't want to loop over the alphabet; you want to loop over the input.

  • count should be initialized to [0]*27 to track "other" values. You can increment counter[-1] for non-alphabetic characters.

  • Your final value is chr of counter.index(max(counter)). You may find it more straightforward, or your teacher may find it more acceptable, to just write a for loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.