If + Count - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: If + Count (/thread-13982.html) |
If + Count - Itay - Nov-09-2018 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 RE: If + Count - ichabod801 - Nov-09-2018 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. RE: If + Count - Itay - Nov-09-2018 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' RE: If + Count - nilamo - Nov-09-2018 That isn't doing what you think it's doing. Have a read: https://python-forum.io/Thread-Multiple-expressions-with-or-keyword RE: If + Count - Itay - Nov-09-2018 (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-expressions-with-or-keywordSo what do I need to do to make the program work well? RE: If + Count - nilamo - Nov-09-2018 (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):
|