Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Str problem, not counting
#1
Hello! I need some help, I really can't understand something. Wall
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:
Error:
File "<input>", line 1, in <module> NameError: name 'a' is not defined
The problem is that 'the program' doesn't step over the input function, so the counting for a, b and c doesn't apply. Cry
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... Angel
Reply
#2
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.
Reply
#3
(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
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.

Thank you A LOT!
I've made some more changes to it so now it looks good. Dance
Reply
#4
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)
Reply
#5
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.
Reply
#6
(Jul-28-2020, 03:46 PM)deanhystad Wrote: 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.

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.
Big Grin
Reply
#7
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?
Reply
#8
(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. Smile
Reply


Forum Jump:

User Panel Messages

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