Str problem, not counting - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Str problem, not counting (/thread-28654.html) |
Str problem, not counting - Hann - Jul-28-2020 Hello! I need some help, I really can't understand something. I try to count how many times 'a', 'b' or 'c' is introduced in 10 repetitions, using an i contor and range function for this. When I debug the py file, says: The problem is that 'the program' doesn't step over the input function, so the counting for a, b and c doesn't apply. Here is the code: count_a = 0 count_b = 0 count_c = 0 char = ['a', 'b', 'c'] choice = '-' for i in range(1, 11): print('Please choose a char from a to c') str(input(choice)) if choice in char: print('You have chosen {}'.format(choice)) count_a += 1 elif choice in char: print(('You have chosen {}'.format(choice))) count_b += 1 elif choice in char: print(('You have chosen {}'.format(choice))) count_c += 1 # else: # print('You have chosen an unavailable char, please try again') print(count_a) print(count_b) print(count_c)Please don't be rough to me... RE: Str problem, not counting - hussainmujtaba - Jul-28-2020 Hy I see you are not storing the input you are getting from the user.Try this count_a = 0 count_b = 0 count_c = 0 char = ['a', 'b', 'c'] choice = '-' for i in range(1, 11): print('Please choose a char from a to c') choice=str(input(choice)) if choice in char: print('You have chosen {}'.format(choice)) count_a += 1 elif choice in char: print(('You have chosen {}'.format(choice))) count_b += 1 elif choice in char: print(('You have chosen {}'.format(choice))) count_c += 1 # else: # print('You have chosen an unavailable char, please try again') print(count_a) print(count_b) print(count_c)Refer to this article about Python incase you need to get your basics right. RE: Str problem, not counting - Hann - Jul-28-2020 (Jul-28-2020, 01:47 PM)hussainmujtaba Wrote: Hy I see you are not storing the input you are getting from the user.Try this Thank you A LOT! I've made some more changes to it so now it looks good. RE: Str problem, not counting - faaadz275 - Jul-28-2020 count_a = 0 count_b = 0 count_c = 0 char = ['a', 'b', 'c'] for i in range(1, 11): print('Please choose a char from a to c') choice = str(input('-')) if choice in char: print('You have chosen {}'.format(choice)) count_a += 1 elif choice in char: print(('You have chosen {}'.format(choice))) count_b += 1 elif choice in char: print(('You have chosen {}'.format(choice))) count_c += 1 # else: # print('You have chosen an unavailable char, please try again') print(count_a) print(count_b) print(count_c) RE: Str problem, not counting - deanhystad - Jul-28-2020 How about a dictionary? count = {'a':0, 'b':0, 'c':0} for _ in range(1, 11): choice = input('Please choose a char from a to c - ') if choice in count.keys(): count[choice] += 1 for key, value in count.items(): print(key, count)If you have a bunch of variables with similar names it often means you should be using a collection. RE: Str problem, not counting - Hann - Jul-28-2020 (Jul-28-2020, 03:46 PM)deanhystad Wrote: How about a dictionary? Thank you! I haven't got to the dictionary lessons on udemy yet, I just read about it a few. In november I'll start a 1 year/twice a week course so I want to learn on my own as much as a I can 'till then. RE: Str problem, not counting - deanhystad - Jul-28-2020 You are taking a Python course and it did not start with Dictionaries???? Dictionaries are so central to Python that it should be named 'Webster'. What does udemy think is more important to Python than dictionaries? RE: Str problem, not counting - Hann - Jul-29-2020 (Jul-28-2020, 07:10 PM)deanhystad Wrote: You are taking a Python course and it did not start with Dictionaries???? Dictionaries are so central to Python that it should be named 'Webster'. What does udemy think is more important to Python than dictionaries? Actually I think it's really very good explained everything. 4h are dedicated to lists and tuples, which I'm learning now, and next are dictionaries and sets. I find dictionaries more complex. |