A value doesn't print()

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:

pencil_icon

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.

2 Likes

@rob42
Yes, x is any number in the defined range (-inf, 1), which was calculated mathematically in this math example: |x - 1| + |x -2| = 6. (Don’t worry about math part for now.)

@Rosuav
Sorry, I got stuck on for x in the range (float(“-inf”), 1), so I can’t write further.

Here is what I’m trying to solve using Python: What’s the value of x?

|x - 1| + |x -2 | = 6

Okay, so you’re going to need to establish some equation-solving techniques, then turn those into an algorithm for finding the solutions. Once that’s settled, you’ll be able to write that algorithm in Python code, and have Python solve the problem for you.

Yes. Python should automatically solve the x value. If the for-loop I wrote can be processed, it would be fine. So there is no solution to my question?
Mathematically, I know the x range: x <=1 or x >=2. (x is in <=1 or >= 2 range; ideally Python should define exact x values.)

Any idea?


xRange = (xRange for xRange in (float("-inf"), 1))
while True: 
    yRange = 1-xRange + 2-xRange == 6
    print(xRange)
    break

error:

    yRange = 1-xRange + 2-xRange == 6
TypeError: unsupported operand type(s) for -: 'int' and 'generator'

I’m sorry, to me that makes no sense whether I interpret it in terms of mathematics (domains are not “in” ranges, and “range of -infinity” means nothing) or in terms of Python.

I guess you are trying to do maths in Python code, in particular, you want to work out infinite sums using Python.

You can’t. End of story. An infinite loop would take an infinite amount of time to run.

Python is a programming language, not symbolic mathematics. You might try Sage or Sympy, but I strongly recommend you learn a little bit of Python first. Especially for Sympy.

The range function is a short-cut for generating objects which are
(approximately) the same as a list of integers:

  • range(10) is equivalent to [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  • range(5, 10) is equivalent to [5, 6, 7, 8, 9]

You can’t build an infinite list in Python, if you converted every atom of matter in the universe into computer memory there wouldn’t be enough, and even if you could, it would take an infinite amount of time to iterate over it one item at a time.

If you want to evaluate some sort of infinite sum like “sum of 2 to the power of negative n for n from 1 to infinity” you either need symbolic maths software (try Sage, or Mathematica) or you need to find a closed form solution without the infinite sum that you can evaluate in Python.