This is the place for queries that don't fit in any of the other categories.

Hello everybody.

I was given a task during a lesson of programming and it has got me stuck. I don't need to do the task but I am not happy not knowing the solution.

Your task is to write a program which counts all the characters (a, e, i, o, u) and all the letters in this text and prints each character as a percentage of all the letters in this string. Remember that space is not a letter.

I think I have the code correct to an extent (aside the percentage part) but when I try to find out how many times a letter appears it always shows 20. Is there anyone who can help me understand this?

Code below:

Code: Select all
`text = "Your task is to write a program"text = text + " which counts all the characters (a, e, i, o, u)"text = text + " and all the letters in this text"text = text + " and prints each character as a percentage of"text = text + " all the letters in this string."text = text + " Remember that space is not a letter."count_a = 0count_e = 0count_i = 0count_o = 0count_u = 0count_letter = 0vowels = count_a + count_e + count_i + count_o + count_ufor character in text:    print (character)    if character == "a" or character == "A":        count_a = count_a + 1        print (count_a)    elif character == "e" or character == "E":        count_e = count_e + 1        print (count_e)    elif character == "i" or character == "I":        count_i = count_i + 1        print (count_i)    elif character == "o" or character == "O":        count_o = count_o + 1        print (count_o)    elif character == "u" or character == "U":        count_u = count_u + 1        print (count_u)    if character >= "a" and character <= "z":        count_letter = count_letter + 1        def question():    print ("Each vowel that appears has been counted.")    print ("Which vowel would you like to view?")    print ("For each vowel, type in the respective letter")    print ("For all vowels, type vowels")    input ("I would like to see ")    if input == "a" or "A":        print ("A appears " + str(count_a) + " times.")        elif input == "e" or "E":        print ("E appears " + str(count_e) + " times.")    elif input == "i" or "I":        print ("I appears " + str(count_i) + " times.")    elif input == "o" or "O":        print ("O appears " + str(count_o) + " times.")    elif input == "u" or "U":        print ("U appears " + str(count_u) + " times.")checkAgain = 'yes'while checkAgain == 'yes' or checkAgain == 'y':    question()    print('Do you want to check another letter? (yes or no)')    checkAgain = input()`
Last edited by metulburr on Sun Mar 30, 2014 9:48 am, edited 1 time in total.
Reason: initial post lock and code tags
kungfool

Posts: 1
Joined: Sun Mar 30, 2014 9:39 am

### Re: Task im stuck on

Code: Select all
`input ("I would like to see ")...if input == "a" or "A":`

This is not how to get input from python. You need to save the input into a variable like:
Code: Select all
`user_input = input ("I would like to see ")...if user_input == "a" or "A":`

This is not what you want:
Code: Select all
`if user_input == "a" or "A":`

You are looking for:
Code: Select all
`if user_input == "a" or user_input ==  "A":`

and you can get rid of that long line by checking only the lowercase:
Code: Select all
`if user_input.lower() == "a":`

then it doesnt matter if the user uppercase's it or not, it will still be converted to lowercase for your if clause.
same with:
Code: Select all
`while checkAgain == 'yes' or checkAgain == 'y':`

Code: Select all
`while checkAgain.lower()[0] == 'y':`

where it converts it to lowercase, and grabs only the first character, so it doesnt matter if the user inputs "yes", "y", "YES", or "Y", it still goes through

also a more refined approach would be to organize the data into a dictionary:
Code: Select all
`vowels = {}vowels = vowels.fromkeys(['a','e','i','o','u'], 0)print(vowels)`

and then just increment the value based on the key
we will be moving to python-forum.io on October 1 2016
more details here

metulburr

Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: Task im stuck on

Some tips,it`s easy to doing thing in a more difficult(unpytonic) way when you are new to coding.
all the letters in this string(Remember that space is not a letter)

Code: Select all
`>>> s = 'test aaaaa'>>> len(''.join(s.split()))9`

which counts all the characters (a, e, i, o, u)

You do want to count all letter in one go.
Code: Select all
`>>> s = 'test aaaaa'>>> s.count('a')5>>> s.count('e')1>>> #All in one go>>> [s.count(letter) for letter in 'aeiou'][5, 1, 0, 0, 0]`

Can also bring in collections Counter.
Code: Select all
`>>> from collections import Counter>>> s = 'test aaaaa'>>> Counter(letter for letter in s if letter in 'aeiou')Counter({'a': 5, 'e': 1})`

and prints each character as a percentage of all the letters in this string

So now you know count of all letter, and count of "'aeiou",so simplified.
Code: Select all
`>>> 5 / 9 * 100 #a55.55555555555556>>> 1 / 9 * 100 #e11.11111111111111  `
We will be moving to python-forum.io on October 1 2016

snippsat

Posts: 1251
Joined: Thu Feb 21, 2013 12:04 am