Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Typeerror argument of type nonetype is not iterable - simple way to solve it
Next
Answered
Dakota Miller
  • 6
Dakota Miller
Asked: May 18, 20222022-05-18T15:47:08+00:00 2022-05-18T15:47:08+00:00In: python

Typeerror argument of type nonetype is not iterable – simple way to solve it

  • 6

. Advertisement .

..3..

. Advertisement .

..4..

I encountered the following problem in completing my work:

File "main.py", line 42, in <module>
  main()
  File "main.py", line 32, in main
  diff()
  File "main.py", line 17, in diff
  gamePlay(difficulty)
  File "/Users/Nathan/Desktop/Hangman/gameplay.py", line 9, in gamePlay
  getInput(word)
  File "/Users/Nathan/Desktop/Hangman/gameplay.py", line 25, in getInput
  if guess in wordInput:

Below is the code I ran:

import random
 
 easyWords = ["car", "dog", "apple", "door", "drum"]
 
 mediumWords = ["airplane", "monkey", "bananana", "window", "guitar"]
 
 hardWords = ["motorcycle", "chuckwalla", "strawberry", "insulation", "didgeridoo"]
 
 wordCount = []
 
 #is called if the player chooses an easy game. 
 #The words in the array it chooses are the shortest.
 #the following three functions are the ones that
 #choose the word randomly from their respective arrays.
 def pickEasy():
  word = random.choice(easyWords)
  word = str(word)
  for i in range(1, len(word) + 1):
  wordCount.append("_")
 
 #is called when the player chooses a medium game.
 def pickMedium():
  word = random.choice(mediumWords)
  for i in range(1, len(word) + 1):
  wordCount.append("_")
 
 #is called when the player chooses a hard game. 
 def pickHard():
  word = random.choice(hardWords)
  for i in range(1, len(word) + 1):
  wordCount.append("_")
from words import *
 from art import *
 
 def gamePlay(difficulty):
  if difficulty == 1:
  word = pickEasy()
  print start
  print wordCount
  getInput(word)
 
  elif difficulty == 2:
  word = pickMedium()
  print start
  print wordCount
 
  elif difficulty == 3:
  word = pickHard()
  print start
  print wordCount
 
 def getInput(wordInput):
  wrong = 0
  guess = raw_input("Type a letter to see if it is in the word: \n").lower()
 
  if guess in wordInput:
  print "letter is in word"
 
  else:
  print "letter is not in word"

What’s causing it, and how can it be resolved in the “typeerror argument of type nonetype is not iterable“ in the python?

typeerror
  • 2 2 Answers
  • 42 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    hdtutoria Expert
    2022-06-10T02:03:39+00:00Added an answer on June 10, 2022 at 2:03 am

    The cause: If a function returns nothing, for example:

    def test():
        pass

    It has a return value of None implicitly.

    As your pick* methods don’t return anything, for example:

    def pickEasy():
        word = random.choice(easyWords)
        word = str(word)
        for i in range(1, len(word) + 1):
            wordCount.append("_")

    the lines that call them, for example:

    word = pickEasy()

    word has been set to None, so wordInput in getInput is also None. That is to say:

    if guess in wordInput:

    has the same meaning as

    if guess in None:

    Because None is an instance of NoneType, which lacks iterator/iteration capabilities, the type error occurs.

    The solution: You can fix this error by adding the return type:

    def pickEasy():
        word = random.choice(easyWords)
        word = str(word)
        for i in range(1, len(word) + 1):
            wordCount.append("_")
        return word
    • 18
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Hugo Chevalier
    2022-05-25T20:25:56+00:00Added an answer on May 25, 2022 at 8:25 pm

    The python error wordInput says wordInput is an iterable. It is actually of NoneType.

    You will notice wordInput if you print wordInput prior to the line in question.

    wordInput is None. This means that the argument passed on to the function is also None. This is word. The pickEasy result is assigned to word.

    Your pickEasy function doesn’t return anything. A Python method that doesn’t return any results in a NoneType.

    You may have wanted to return word. This will suffice.

    def pickEasy():
     word = random.choice(easyWords)
     word = str(word)
     for i in range(1, len(word) + 1):
     wordCount.append("_")
     return word
    • 11
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.