Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If + Count
#1
Hello , I'm a python beginner and I need help with For + Count.

I need to create a program that inputs a text , and then it count how many times the letter A, B, C appears in the text.
If A appears the most it needs to print a
If B appears the most it needs to print b
If C appears the most it needs to print c
If there isnt any a,c,b in the text or the 3 letters appear at the same amount it needs to print Balanced

I need help with the last IF i mentiond and I also need help with the 3 other IF'S because if I input 'ACC' it will print a and not c
text = raw_input ('Enter a text:')
count_a = 0
count_b = 0
count_c = 0
for letter in text:
    if letter == 'a':
        count_a += 1
    if letter == 'b':
        count_b += 1
    if letter == 'c':
        count_c += 1
if count_a > count_b or count_c:
    print 'a'
elif count_b > count_a or count_c:
    print 'b'
elif count_c > count_a or count_b:
    print 'c'
else
    print 'balanced'
I also tried using AND instead of OR
Reply
#2
To check that a is the maximum, you need to be sure that it is greater than b and greater than c. You are only checking if a is greater than b. So the comparisons after the loop each need to have to inequality comparisons joined by the and operator.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Is this what you mean?
text = raw_input ('Enter a text:')
count_a = 0
count_b = 0
count_c = 0
for letter in text:
    if letter == 'a':
        count_a += 1
    if letter == 'b':
        count_b += 1
    if letter == 'c':
        count_c += 1
if count_a > count_b and count_c:
    print 'a'
elif count_b > count_a and count_c:
    print 'b'
elif count_c > count_a and count_b:
    print 'c'
else
    print 'balanced'
Reply
#4
That isn't doing what you think it's doing. Have a read: https://python-forum.io/Thread-Multiple-...or-keyword
Reply
#5
(Nov-09-2018, 06:36 PM)nilamo Wrote: That isn't doing what you think it's doing. Have a read: https://python-forum.io/Thread-Multiple-...or-keyword
So what do I need to do to make the program work well?
Reply
#6
(Nov-09-2018, 03:23 PM)Itay Wrote: if count_a > count_b and count_c:
That depends on what your intention is with that code. Right now, it's the same as this: if (count_a > count_b) and (count_c > 0):
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Count Letters in a Sentence (without using the Count Function) bhill 3 5,065 Jun-19-2018, 02:52 AM
Last Post: bhill

Forum Jump:

User Panel Messages

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