def infi ():
negativeInfi = float("-inf")
possitiveInfi = float("inf")
Thanks.
def infi ():
negativeInfi = float("-inf")
possitiveInfi = float("inf")
Thanks.
range(item<0,item>=0)
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.
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")
else:
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)
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.
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.
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.