1

I am currently using sublime text 3. I am trying to prompt the user for a string, check the string length and store that string, however I run into "NoneType error" when I try it. It currently looks like this.

from cs50 import get_string

while True:
            name_input = get_string("Welcome to Ocorn, a text-based RPG.\nChoose a name between 1 and 10 characters: ")
            if len(name_input) <= 10 and len(name_input) >= 1:
                break

player.name = name_input
pnuk23
  • 11
  • 3
  • is `player` assigned somewhere else? – Jordan Bonitatis Dec 08 '17 at 05:17
  • Yeah it is, it's defined as a function, but that is not the issue. – pnuk23 Dec 08 '17 at 05:31
  • "It is defined as a function" sounds like it really could be the issue. What do you mean? Is player a function? Or is it defined inside a function? In both case, that won't work. My bet is that you defined something like `player=None` in this scope, then assigned something to player by doing `player=Player()` but inside a function. Note that this will not work because the second assignation is done in the scope of the function and will be lost when it returns. – Olivier Melançon Dec 08 '17 at 07:05
  • It doesn't matter because when I delete that variable, the error is still raised. It is raised a few lines before it in "if len(name_input) <= 10 and len(name_input) >= 1:" – pnuk23 Dec 08 '17 at 15:46

1 Answers1

-1

get_string belongs to the cs50 library so if you tried to do it in without importing the library it won't work.

The syntax for including a library or a function is to use import, and we are importing get_string from the cs50 library, which was pre-installed on the CS50 IDE.

https://docs.cs50.net/2017/fall/notes/8/lecture8.html

If you're in ubuntu you can do the following

  1. Download the python3 $ sudo apt install python3
  2. Download the python3-pip $ sudo apt install python3-pip
  3. Download the cs50 library $ sudo pip install cs50
  4. Import the needed method in .py file an example hello.py file
from cs50 import get_string

answer = get_string( "What is your name?\n")
print( "hello, " + answer)

  • This doesn't answer the question. The OP showed that they know how to use `import`. The problem is that the `get_string` method isn't behaving like they think it should (i.e. it causes a `NoneType` error). – Matthew Cole Sep 22 '20 at 16:08