Domanda

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?

È stato utile?

Soluzione

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.)

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top