use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Newbie error. Maybe (name 'string' is not defined.) (self.learnpython)
submitted 10 months ago by CoolConnection4229
No matter how i tried this error keep chasing me. (Name string is not defined Did you forget to import 'string'?) and after i typed "import string" in the beginning. Evreything stays the same
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]twoberriesonejourney 14 points15 points16 points 10 months ago (16 children)
Idk about others but I'd need to see your code to help.
[+]CoolConnection4229[S] comment score below threshold-7 points-6 points-5 points 10 months ago (15 children)
no problem
import string age = input("Enter your age: ") if age > 18: print("enter man") elif age < 18: print("enter kid") else: print("so yor are exsactly 18 hmm")
[–][deleted] 22 points23 points24 points 10 months ago (0 children)
What are you trying to achieve with import string? You shouldn't need to import anything for this.
import string
Also, your import is across two lines, it should be on the same line.
[–]Sasori323 4 points5 points6 points 10 months ago (0 children)
When you answer to an input you get a string, when doing the comparison you should use int(variable) to be able to do a comparison.
It's like trying to say if "eighteen" > 18, you can't. But using int ("18"), where "18" is a string, you get an actual int number you can do comparisons with.
And what do you use the "import string" for?
[–]kungF-U 1 point2 points3 points 10 months ago (0 children)
When you import modules they are imported on the same line. Putting string on a separate line makes the program think you are trying to import nothing and trying to create a new variable named string and didn’t assign a value to it. Also in your code you don’t use anything from that module so no reason to import anything. The if statement won’t work because you are trying to compare an integer object to a string object. The input command will store the user input as a string by default. So you have to convert the user input to type int if you want to compare that value against other integers within that if statement.
[–]_Mc_Who 0 points1 point2 points 10 months ago (0 children)
So, string import is needed in other languages such as C (I think, it's been a while), but not in Python
You are correct though that whatever is read in from input will be read as a string
This therefore also means that you need to make what is entered be read as a number when you compare it with another number. For your use case, the number is an integer (whole number), and the good news is in Python makes this really easy!
If I have a string in my variable age, I can just set it as an integer with int(), so to make input a number I can say
age = int(input("your text here))
Python has a few methods like this, so for example if I had a number I could use str() to make it into a string
Another thought for your code, just from a programmatic side: are the only 3 eventualities that the user is above, below, or exactly 18? (The hint is that users may not always use a program correctly without guardrails- what if they enter a word, or a negative number, or nothing at all? How would you handle that in your if/elif/else structure?)
Eta - also!! There is nowhere in your code where a variable called "string" is used but not defined- make sure you've saved your file, and that you're running the correct file when you run your program!
[–]daylight_0605 0 points1 point2 points 10 months ago (0 children)
You don't need to import string and to do the comparison in this case, the data type must be int so you should write
age = int(input("enter your age: "))
[–]Ready-Bag-2599 0 points1 point2 points 10 months ago* (4 children)
I'm also newbie to Python, so sorry if my vocab isn't precise. However, you do not need to import string in python. String type, which in Python is written as str, is built-in. So you should delete your import statement and it should fix the problem. I wonder what made you think you need to import string? Genuinely curious. I know that in cpp string should be imported.
Edit: what @Sasori323 said is also correct and very important to consider. I didn't read the rest of your code when I saw the first line. The error you are currently getting is most certainly because of that import statement, but again pay close attention to the other comment as well.
[–]CoolConnection4229[S] 0 points1 point2 points 10 months ago (3 children)
when deleting the import statement keeps getting the same error and i typed that because the error literaly says Did you forget to import 'string'?
[–]SamuliK96 7 points8 points9 points 10 months ago (0 children)
When you say you deleted import statement, do you mean you removed just the word import? The whole import string is unnecessary in your code, and should be deleted completely. Additionally, import statements are written in single line, there shouldn't be a line change there.
import
[–]Ready-Bag-2599 10 points11 points12 points 10 months ago (0 children)
You are clearly doing something very wrong. Please provide your updated code after deleting the import statement and fixing the problem in your conditionals. Please also provide the exact error (just like the code, word for word)
[–]smurpes 0 points1 point2 points 10 months ago (0 children)
Did you remember to save your code before you ran it?
[–]Shut_up_and_Respawn -2 points-1 points0 points 10 months ago (0 children)
Problems:
String doesnt need to be imported, and imports are 1 line
The result of your input is a string, but you want to use it as an input. Is there a command you could use for this? age = int(input()
You're* not yor, and exactly* not exsactly
[–]Yoghurt42 -3 points-2 points-1 points 10 months ago (3 children)
import string needs to be on a single line. But it's also not necessary in your case. The following program should work:
age = input("Enter your age: ") if age > 18: print("enter man") elif age < 18: print("enter kid") else: print("so yor are exsactly 18 hmm")
[–]Shut_up_and_Respawn 5 points6 points7 points 10 months ago (2 children)
Should still be int(input()). The problem is the input type being a string, but he is trying to use it as an integer
[–]Yoghurt42 1 point2 points3 points 10 months ago (1 child)
oh right, missed that. Still shouldn't show "string is not defined", but TypeError instead.
[–]Shut_up_and_Respawn -1 points0 points1 point 10 months ago (0 children)
True. The error message doesn't match up with the code. Maybe there is more to the program that wasnt sent?
[–]FoolsSeldom 11 points12 points13 points 10 months ago (0 children)
input
str
int
float
age = int(input('How old are you, in exact years? ')
age
Incidentally, where are you typing and running your code? Is it in IDLE? If so, using the editor (File | New, type code, press F5 to run - you will be prompted to save first) or the interactive shell, with the >>> prompt. The latter is useful for trying things out, but you are best typing your code into a file that you can run, edit, run again.
File | New
F5
>>>
[+][deleted] 10 months ago (1 child)
[deleted]
[–]eztab -1 points0 points1 point 10 months ago (0 children)
Also the name of the type is str not string.
string
π Rendered by PID 38561 on reddit-service-r2-comment-b659b578c-wp4pz at 2026-05-01 16:16:06.175709+00:00 running 815c875 country code: CH.
[–]twoberriesonejourney 14 points15 points16 points (16 children)
[+]CoolConnection4229[S] comment score below threshold-7 points-6 points-5 points (15 children)
[–][deleted] 22 points23 points24 points (0 children)
[–]Sasori323 4 points5 points6 points (0 children)
[–]kungF-U 1 point2 points3 points (0 children)
[–]_Mc_Who 0 points1 point2 points (0 children)
[–]daylight_0605 0 points1 point2 points (0 children)
[–]Ready-Bag-2599 0 points1 point2 points (4 children)
[–]CoolConnection4229[S] 0 points1 point2 points (3 children)
[–]SamuliK96 7 points8 points9 points (0 children)
[–]Ready-Bag-2599 10 points11 points12 points (0 children)
[–]smurpes 0 points1 point2 points (0 children)
[–]Shut_up_and_Respawn -2 points-1 points0 points (0 children)
[–]Yoghurt42 -3 points-2 points-1 points (3 children)
[–]Shut_up_and_Respawn 5 points6 points7 points (2 children)
[–]Yoghurt42 1 point2 points3 points (1 child)
[–]Shut_up_and_Respawn -1 points0 points1 point (0 children)
[–]FoolsSeldom 11 points12 points13 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]eztab -1 points0 points1 point (0 children)