Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
int is not iterable
#1
import random
    easier = ["joke","haze","kick","apex","buff","exam","mope","pick"]
    words = (random.choice(easier))
    first=input ("Ok choose your first letter")
    for char in words:
        amount = words.count(char)
        if first in amount >= 1:
            print(amount)
I'm trying to make hangman, but I'm trying to find out how to make it check how many times a letter(that's inputed by the user)is in the randomly generate word. Please help me. Smile
Reply
#2
It would be more simple to just first make a database of the count of letters in your list. Then compare the users letter with the database


words = ["joke","haze","kick","apex","buff","exam","mope","pick"]
d = {}

for word in words:
    for letter in word:
        count = word.count(letter)
        try:
            d[word][letter] = count
        except KeyError:
            d[word] = {}
            d[word][letter] = count
print(d)
Output:
{'joke': {'j': 1, 'o': 1, 'k': 1, 'e': 1}, 'haze': {'h': 1, 'a': 1, 'z': 1, 'e': 1}, 'kick': {'k': 2, 'i': 1, 'c': 1}, 'apex': {'a': 1, 'p': 1, 'e': 1, 'x': 1}, 'buff': {'b': 1, 'u': 1, 'f': 2}, 'exam': {'e': 1, 'x': 1, 'a': 1, 'm': 1}, 'mope': {'m': 1, 'o': 1, 'p': 1, 'e': 1}, 'pick': {'p': 1, 'i': 1, 'c': 1, 'k': 1}}
Then you just input what the user into the dictionary to give the count
import random
words = ["joke","haze","kick","apex","buff","exam","mope","pick"]
d = {}

for word in words:
    for letter in word:
        count = word.count(letter)
        try:
            d[word][letter] = count
        except KeyError:
            d[word] = {}
            d[word][letter] = count
print(d)
chosen = random.choice(words)
search = input(f'search for letter in {chosen}: ')
print(d[chosen][search])
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question how to solve `'TypeError: 'int' object is not iterable`? netanelst 2 1,521 May-24-2022, 12:03 PM
Last Post: deanhystad
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,095 Aug-10-2021, 08:10 AM
Last Post: snippsat
  Cannot unpack non-iterable NoneType object, i would like to ask for help on this. Jadiac 3 8,815 Oct-18-2020, 02:11 PM
Last Post: Jadiac
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 5,864 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  Why the result of "extended iterable unpacking" with set() is unpredictable? zohanlin 2 2,015 Jun-29-2020, 10:30 AM
Last Post: zohanlin
  TypeError: 're.Match' object is not iterable charlesauspicks 1 11,488 May-25-2020, 06:14 AM
Last Post: bowlofred
  function/nonetype object is not iterable nanok66 5 3,965 May-08-2020, 07:39 PM
Last Post: nanok66
  min() function in iterable index OokaydO 4 2,908 Apr-23-2020, 09:21 AM
Last Post: OokaydO
  'int' object is not iterable el_bueno 2 2,649 Feb-18-2020, 06:25 PM
Last Post: Larz60+
  What is the correct type hint when you want to accept Iterable but not Dictionary LadySvetlana 4 3,251 Mar-05-2019, 07:33 PM
Last Post: LadySvetlana

Forum Jump:

User Panel Messages

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