0

I have this code as an example:

for i in func(x):
    count = 1
    while i > 0:
        if count % 2 == 0:
            print("(:")
            count += 2
        else:
            print("(:")
            count += 2

Or this one:

for i in func(x):
    count = 0
    if count % 2 == 0:
        print("(:")
        count += 1
    else:
        print(":(")
        count += 1

They don't work as I want them to work.

The first one just enters in an infinite loop, the second one only prints the happy smiley I want it to print the happy smiley then the sad smiley an so on

This is the original code

9
  • You didn't tell us what result you want to get and you telling us 'They don't work as I want them to work.'. So how do you expect us to know what you trying to produce? We don't have psychic mind reader ability. Commented Apr 2, 2014 at 3:51
  • @Sky I updated with the result I want Commented Apr 2, 2014 at 3:52
  • 1
    Does func(x) ever return a number <=0? Also the first loop will always hit the else condition. Commented Apr 2, 2014 at 3:57
  • Need to know what's func(x). The problem seems to be in there. Commented Apr 2, 2014 at 3:58
  • @Sky def gruseis(num): bina6 = int(num) _bina6 = bina6 if bina6 != 0: while bina6 > 0: bina6 %= 1000000 yield bina6 bina6 = _bina6 // 1000000 _bina6 = bina6 else: bina6 = "0" yield bina6 sorry, don't know how to comment with code Commented Apr 2, 2014 at 4:00

2 Answers 2

1

You probably want the enumerate() function. It appears that i is never used, but that is actually irrelevant.

The enumerate function takes an iterable (so anything you can loop over) and returns a tuple with the count and each item in order.

for index,item in func(x):
    if index % 2 == 0:
        print("(:")
    else:
        print(":(")

This way, even if you don't use i (or in the above case item) yet, you have the option of doing so in the future.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you're trying to use i and count as two different values to use while iterating, and I don't think you need to do this to get what you want.

First of all - a while statement is basically a loop that runs until it is False. So if you have

green = True
while green:
    print("Hello")

...this will never end. Placing the while in a for loop doesn't change that; it simply waits for the while my_condition to turn False.

Additionally, in your first loop you are incrementing count -- but the while loop is looking at the value of i, which never gets a chance to change because the while never becomes untrue!

Finally - in your second loop, you're setting count at the beginning of each iteration! This means that count will never actually progress in a meaningful way; you set it to 0, print an emoticon, increment it by 2, then continue in the for loop - resetting count to 0!

Basically, just realize that you can use i directly, there's no need for a count variable as well. for i in something will iterate over whatever is in something if it is an iterable thing. So for example:

for i in range(0, 10):
    if i % 2 == 0:
        print(":)")
    else:
        print(":(")

EDIT: After seeing the original code, it looks like you are trying to produce a generator object.

Using yield in conjunction with while or for will net you a generator - an object you can iterate over. Here's a baseline example of that.

>>> def baz(x):
...     i = 0
...     while i > x:
...             yield i
...             i += 1
>>> baz(4)
<generator object baz at 0x1004935a0>
>>> me = baz(4)
>>> me.next()
0
>>> me.next()
1
>>> me.next()
2
>>> me.next()
3
>>> me.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> 

In a situation where you just want to generate a set number of objects, you can skip the while and just write a for, which will save you a couple of lines. For example, the following code does the same as the above:

def foo(x):
    for i in range(0, x):
        yield i

However, in the example you've shown me there seems to be some more involved math during the while loop, so it does make sense to use a while sometimes. Imagine a situation where you wanted to exit the loop when i became prime, for example…

def until_prime(x):
    while is_not_prime(x): ## or, 'while not is_prime(x)', whatever our imaginary function does
        x = x * (x + 1)
        yield x

I have no idea if the code above will ever generate a prime number, so please don't judge it too harshly. :D Nevertheless, it is an example of a situation where you couldn't necessarily write a for loop, so you'd need to wait until a condition arose.

1 Comment

+1 for explaining why the first loop never ends, but if i is relevant at a later stage, the enumerate answer I put below might be useful too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.