-2

I have been 'flagged' for posting a duplicate question. However, my question was not the same, as the other coder was asking how to "count how many occurrences there are of specific characters in a string". My output was not counting the occurrences of specific characters, but counting a total of specific characters (all vowels) in the string. For example the other guy was looking for the output to be:

A:1 E:6 I:2

I was also looking for a total of the numbers, so my output should be: 9 (granted we are speaking about the same string in both codes)."

I have created a piece of code but for some reason it is counting all letters, not just the vowels. I cannot figure out what part I went wrong at.

vowels = 'a', 'e', 'i', 'o', 'u'

ip_str = raw_input("Enter a string: ")
ans = str(raw_input)

count = (0)

for letters in ans:
    if letters in vowels:
        count += 1

print str(count)
Will
  • 24,082
  • 14
  • 97
  • 108
  • 2
    You will need to include your code as text and not as a link to an image. – shuttle87 Jan 23 '16 at 19:32
  • 3
    Instead of posting a screenshot, you should post the actual text – Sayse Jan 23 '16 at 19:34
  • well you never use `ip_str` – M4rtini Jan 23 '16 at 19:34
  • 1
    Possible duplicate of [Count Vowels in String Python](http://stackoverflow.com/questions/19967001/count-vowels-in-string-python) – timgeb Jan 23 '16 at 19:42
  • and more to the point, you cast raw_input (a function) to a string and use that instead of ip_str – Foon Jan 23 '16 at 21:13
  • Sorry, this is my first time using this website. I looked around to see if anyone had already asked this question, and I thought that others had posted screenshots of their code. Next time I will post as text. I didn't use ip_str because I was confused and thought that it would count the raw_input answer with the "Enter a string: " vowels and that I could use ans to keep the input that was collected separate from the string. I don't know if that makes sense, but in my head I thought it did. I am very new to this and trying really hard to learn and understand it. – Mackenzie Jan 25 '16 at 00:33
  • And @timgeb I searched the website before I posted my question, and no question or responses gave me any clarification to my problem. I have edited my post with the differentiation between the other coders issue and mine. I understand I used the same title as the other question, but they are in fact different. I am sure if y'all took a minute to look at the other coders question, y'all would be aware of that as well. Like I've said previously, I'm new at this so a little more understanding would be appreciated. Thanks. – Mackenzie Jan 25 '16 at 00:39

4 Answers4

3

You store the input in the ip_str variable but you never actually use that variable again. You want to be using that variable when iterating. Note that in your code when you have:

>>> str(raw_input)
'<built-in function raw_input>'

this actually gets the name of the builtin function and stores that in ans and not your input. So every time you run you are getting the number of vowels in '<built-in function raw_input>' instead of your input, this will always be 9.

shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • Thanks! I appreciate your answer. Ironically enough I was testing the code with my name "Mackenzie" and that has 9 letters as well so I assumed it was counting all letters. I would rate your answer up but I don't have any reputation yet. This was my first question asked. But thanks a million, that explained a lot! – Mackenzie Jan 25 '16 at 00:09
0

I have modified your code a little bit and this works completely fine for me.

vowels = 'a', 'e', 'i', 'o', 'u'

inp = input("Enter a string: ")

count = 0

for letters in inp:
    if letters in vowels:
        count += 1


print("Count: " + str(count))
Walshy
  • 850
  • 2
  • 11
  • 32
0
raw_input("string") 

This is a function that takes a string as parameter, prints that string, gets input from the user and then stores that in the valueable you store the entire function( as in: variable1 = raw_input("string") will prompt you with "string" and then waits for your input, then stores that input in variable1)

Dennis Lukas
  • 483
  • 2
  • 6
  • 20
0

We have a few things wrong here. First, let me correct it, and then I'll explain:

vowels = ('a', 'e', 'i', 'o', 'u')

ans = raw_input("Enter a string: ")

count = 0
for letters in ans:
    if letters in vowels:
        count += 1

print str(count)

raw_input is a function, not a variable. I think you meant str(ip_str), but there's no need to do that, since raw_input will always return a string:

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

So, we can just directly set ans = raw_input("Enter a string: ").

Now, for a couple of matters of style: vowels is a tuple, and putting ()'s around it makes that more clear. It will work fine either way. count is just a simple int, and doesn't need any ()'s around the definition.

In short, though, the only thing actually wrong with it was how you were defining ans.

Will
  • 24,082
  • 14
  • 97
  • 108
  • I am so thankful for your input and for you taking the time to help me out! This was very useful and helped me understand where I messed up at. Now the code works perfectly and I am so grateful! I kept staring at it but had "writers block" and couldn't understand where I was going wrong. I am very new at this but am trying to work really hard to learn and understand it. Thanks a million! I would vote up your answer but unfortunately I don't have any reputation yet. This was my first question posted on this site. Thanks again! – Mackenzie Jan 25 '16 at 00:14
  • I missed your comment before, but thank you! Glad to help :) – Will Jul 04 '16 at 04:27