A value doesn't print()

def infi ():
    negativeInfi = float("-inf")
    possitiveInfi = float("inf")



item<0 is a Boolean value telling you if item is less than 0. In your example it’ll be False. item>=0 is the opposite and will be True.

Therefore you’re looping over the range(False, True) (basically equivalent to range(0, 1)). You’re looping in steps of 1.

Since it’s a half open range it doesn’t include the last value, so the loop will be performed once, on the value False


Given what you have here:
if 1 == 6: ← will never be True
if 2 * item == 9: ← will never be True
else: print("this code is wrong") ← will always be True

Also, item does have a ‘value’, which is changed from the initialize value of 0.0 by the for: loop; it’s just that the above logic does not permit said value the be passed to the print() function.

1 Like

Let us re-write that code so we can see what is happening:

x = float()
print("x =", x)
r = range(x < 0, x >= 0)
print(r)  # So you can see the values in the range object.
for item in r:
    print(item)  # So you can see the values in the loop.
    if 1 == 6:  # Always False
        # Dead code that will never execute.
        print("This will never happen")
    if 2*item == 9:
        # 2*0 is 0, not 9, so this will never execute.
        print("This will also never happen")
        print("This code is correct since", 2*item, "is not equal to 9")

I think you might be having trouble understanding for-loops. Try these:

for item in [3, 5, 2, 4]:
    print("item =", item)

for char in "Hello":
    print("char =", char)

for value in range(10):
    print("value =", value)

for x in range(2, 6):
    print("x =", x)
1 Like

Why have you completely changed your first post??

Now, the thread makes little to no sense.


In general, it is best to refrain from making substantial changes to a post after others have responded to it, particularly if it obfuscates the meaning of the thread. In the current case, the changes that have been made to the original post render the discussion difficult for the reader to understand.

Those who read this discussion should note that they can view the history of edits made to a post by clicking the pencil icon located at the upper right corner of the post. It currently appears as follows for the original post:


The orange color of the pencil will gradually fade to gray during the course of the day, however the history will remain available.

1 Like

Thank you: i did not know that.

I found the question to be stupid : \ That’s why…

If you don’t know what you did wrong in your code, and now (given the answers posted) you’ve learned something, then there’s nothing stupid about it.

1 Like
def infi ():
    negativeInfi = float("-inf")
    possitiveInfi = float("inf")
    return negativeInfi,possitiveInfi  
var = infi() 
for x in range (float("-inf"), 1):

Q1: Line var = infi():
How would you call out a single value within def()? In this case, either negativeInfi or possitiveInfi

Q2: Line for x in range (float(“-inf”), 1): >> error: ‘float’ object cannot be interpreted as an integer.
How would you make it work? As you can see, I’d like to define (-inf to 1).

Let me take the second part first: the range() function.

That function takes integer values as its arguments, as per the error message.

For your first question: simply assign the return values to two variables: neg_inf, pos_inf = infi()

In fact, I already explained that concept to you, in this thread

I have a question for you: why are you using an abstract concept, namely "inf", for this? Why not use positive integers, until you understand the basic concepts of Python?

Wait a minute. How do you iterate from negative infinity to 1? Never mind about the range function or the for loop, or anything at all in Python; how do you iterate from negative infinity?

I’m evaluating math formulas, which often times use negative values and/or -inf().

I don’t think Python allows that. Neg_inf is just a concept. In this case, I’m trying to find a domain in the the range of neg_inf

That’s fine, but you can evaluate math formulas, using positive integers, while you learn Python. By doing that, you’ll learn Python by applying what you already know about math formulas.

for x in range (float(“-inf”), 1): How would you solve this since for-loop doesn’t take float()?

"-inf" is an abstract. How can you have a range within a abstract?

Even if that was not the case, and we could define infinity, the range would go on (or back?) for ever.

If you want an infinite loop, just do:

while True:
    # the rest of the code

Since for x in range (float(“-inf”), 1) cannot operate, so I can’t use while True statement, meaning the rest of the code I’m going to write won’t operate.

So, what is x? Any number?

You can use while 1:, or while -1: or ‘while’ anything that is not zero, to create an infinite loop.

Negative values aren’t a problem, but iterating is. Can you do a summation in mathematics saying “sum of n = -∞ to 1, [some formula involving n]”? (Sorry, I don’t think I can embed MathML in Discourse or I’d do the proper sigma notation.) It doesn’t make sense to iterate over a range like that.

So if you aren’t intending to iterate, what ARE you doing? All you’re posting is code that doesn’t work, and giving us no clue whatsoever as to what you’re actually trying to accomplish.