r/PythonLearning • u/Moral_Roulette34 • Jul 02 '25
[help needed] for i in range commands not valid?
I really don't know what this means, could someone please explain and show me how to fix it?
17
u/RUlNS Jul 02 '25
just a side note but you could just do this:
target = int(input(“enter a number”))
if target in numbers:
print(“found it”)
else:
print(“not here”)
9
2
u/F100cTomas Jul 02 '25
print("found it" if (found := int(input("enter a number ")) in numbers) else "not here")
8
u/ManGuy0705 Jul 02 '25
squeezing it into a one liner does not make it better code
2
u/lucdewit Jul 04 '25
okay okay but what if its EVEN SHORTER? print("found it"if int(input())in n else"not here")
1
1
1
u/rorschach200 Jul 05 '25
In fact it usually makes it worse, which makes languages that allow and advertise a lot of features like this in fact worse in most professional environments as in most professional environments a large - and often the largest - portion of code is written by developers who are either inexperienced or not very well trained, or both, and those typically have the tendency of acting as if using as many different language features available as possible makes the code better.
Sometimes that does make the code shorter (and worse), sometimes that in fact makes the code much, much longer and bloated (and worse) - think not in terms of lines, but in terms of unnecessary class hierarches and interfaces and long chains of passing data from one unnecessary object to another unnecessary object, in both cases though it makes it harder to read the code (especially quickly while just scanning through the code) or edit it later when requirements change and modification is necessary, or to fix a bug.
There is a reason why Go is the way it is - it was developed by Google primarily for internal use to address internal problems, such as the problem above (as well as others) - and there is a reason why experienced, well trained senior professionals by now often would prefer modern, recent-version Java to Kotlin for a big project that for one reason or another requires use of a JVM language - Kotlin is all fun and games, but it's full of features that inexperienced developers use for no good reason despite lower readability and higher editing cost of the results, while the last few updates to Java (that came out long after Kotlin reached 1.0 status) were incredibly well thought out and they effectively included top 10% of Kotlin features into vanilla Java, where "top" is defined not by popularity or excitement generated among less experienced developers, but by their utility and degree of long term positive impact in real projects in the industry. Making vanilla Java just a better choice for anything serious, where "better" is defined by long term health of the project and people's productivity, not "more fun for youngsters".
1
u/rorschach200 Jul 05 '25
There is another bug in in the original code (cc u/Moral_Roulette34 ): the inner `for` loop is only executed 10 times (for i from 0 (inclusive) to 9 (inclusive)), which means `count` variable can be at maximum incremented 10 times, which means increased by 10, and it starts at 0. Therefore, `count` variable never exceeds 10.
And if the target is not in the list, the only way the outer `while` is ever exited if count reaches 11, which it can not. So if the target is not in the list, the original code from the post hangs.
10
u/fredhamptonsaid Jul 02 '25
I'm a beginner too. Did you Google the error message? It says you're treating an int as a function.
What does it say when you hover over range(10)?
7
8
u/ninhaomah Jul 02 '25
why is your target a string and not int ?
and where does numbers come from ?
and what happened to i ?
6
u/FuzzySloth_ Jul 02 '25
You must have defined "range" as an integer elsewhere in the code before the line 40. Check for it and change the variable name to something else, and run the code again.
1
u/Moral_Roulette34 Jul 02 '25
I've switched to using a while loop instead and it works now but thanks all!
7
u/TriscuitTime Jul 02 '25
I hope you understand why the error happened in the first place, still. You have to be careful naming variables with the same name of built-ins, like “if” or “while” or “range” or “list”
0
u/fredhamptonsaid Jul 02 '25
Did we get it confirmed that they used a variable named range? I was just trying to keep up with the troubleshooting so I can learn.
3
u/TriscuitTime Jul 02 '25
Not confirmed, but deduced with basically 100% certainty. This is the exact error message you would expect if you had set range to an integer value before trying to use the built-in
1
0
Jul 04 '25
[deleted]
1
u/TriscuitTime Jul 04 '25
The error explicitly points to the “range(10)” as the point of failure as well as stating that an ‘int’ object is not callable, so the redefining of “range” is definitely the issue
2
u/Vincent_Van_Goooo Jul 02 '25 edited Jul 02 '25
You should still really look at the comments... Some people have pointed out this can just be done in one if statement, without a loop, and even if it wasn't possible to do it without a loop you only need one to do it. You're adding on extra steps that add complexity and use more memory.
How it should be done:
num_list = [0,1,2,3,4,5,6,7,8,9,10] if int(target) in num_list: print("number is in list") else: print("number is not in list")If for some reason you INSIST on using a loop it would be something like:
check = 0 for i in range(0,10): if I == int(target): check = 1 break if check: print("number is in list") else: print("number is not in list")Two switches do the job, with half as many checks as you're going to be using. Considerably less variables, means less memory usage, only one loop (cause your second loop didn't really do anything) and fyi unless there's some weird edge case where you MUST use a while loop always default to for loops. They're faster and use less memory.
2
u/Able_Mail9167 Jul 02 '25
The error message is telling you "int is not callable". This means that python thinks 'range' is a variable with an integer in it, not a function.
Check earlier bits of your code and see if you accidentally created a range variable that's shadowing the range function.
1
u/Buttleston Jul 02 '25
Does it work when you run the program?
Do you have some other function or variable named range in this file, or imported?
1
u/MysticClimber1496 Jul 02 '25
Side note you could change your ‘Found’ variable to ‘notFound’ and then your while statement is simpler,
While notFound {
notFound = False // you found the thing
}
1
u/West-Map-8433 Jul 02 '25
Just put not between while and Found, and you don't have to change the variable anyhow
While not Found:
Blabla
1
1
u/Murphygreen8484 Jul 02 '25
What is the purpose of this code? (If the purpose is just to better understand loops that's fine). I only ask because there are better ways of doing this if this was production code.
1
u/Zealousideal_Yard651 Jul 02 '25
Your error message says that "Int is not a callable". This tells us that range is an integre and not function. How can that happend?
Well, somewhere in your script you have put something like: range = 10 thus changing the standard function into a variable with int value. To fix this just rename that variable from range to something else so it doesn't overwrite the built-in function.
1
u/cancerbero23 Jul 02 '25
It seems like you have defined another variable called "range" before or imported a variable called "range" from somewhere.
Plus, instead of using "found == False", try to use "not found".
1
1
u/Illustrious_Post6048 Jul 03 '25
Target is a string not an integer! Convert into integer and also i think you have made a variable named range elsewhere in the code. Rename it to something else and then you should be good to go
1
u/Saajaadeen Jul 04 '25
I'm super late but here is my take on it
t = int(input("Enter a number to search for: "))
n = range(10)
found = False
for i in n:
if i == t:
found = True
if found == True:
print('Found!')
else:
print('Not Found...')1
u/the_Elric Jul 04 '25
Why not just assign range to a variable and then run the loop logic on said variable? Just curious…..
1
Jul 06 '25
I guess you can do it like
while found:
for i in range(10):as found is already is False which is True
and also check the indentations
0
u/josemeek Jul 02 '25
While found: For i in range(10): // this iterated from 0 - 9
Assuming that number is 10, then it is beyond range
1


25
u/SirCokaBear Jul 02 '25
Did you make another variable earlier in your code called range?