0

I'm having difficulty making this simple if statement work. I'm trying to get a string to print for a set amount of times using an 'if' statement

x = 0
if x < 9:
    print "this works"
    x = x + 1
else:
    print "this doesn't work"

The result of the code above is that it prints "this works" only once. I want 1 to be added to x until x reaches 9 and the statement is not longer true.

Can anyone help me here?

2
  • Use for loop to count, you can not just count to 9 with og statement ,or make a recurrtion call Commented Feb 23, 2016 at 19:27
  • If you have code and/or a question is about a certain language, tag the language. It not only helps users find the question, but also affects syntax coloring. Commented Feb 23, 2016 at 19:28

3 Answers 3

5

I think what you're asking for is a while statement. Try this:

x = 0
while x < 9:
    print "this works"
    x = x + 1

print "this doesn't work"

An if statement does not loop. You can think of it in the sense of English:

  • "If something is true then do something else do something different" - no implied do something multiple times in this statement.
  • "While something is true do stuff." - here "while" suggests that something may happen multiple times.

Other ways of doing this:

If all you're looking to do is loop through something with the values x 0-9 then a for loop would work well:

for x in range(9):
    print "this works"
print "this doesn't work"

In this case range(9) can be replaces by range(start_value, stop_value+1) for more genericism. This is more typical way of doing this, I suggested while merely because it sounded most like what you were thinking if would do.

Another way:

x = 0
while True:
    if x < 9:
        print "this works"
        x = x + 1
    else:
        print "this doesn't work"
        break

Based on Adam's solution. Not generally used for something where you can easily work out the end condition, but can be useful where the end condition isn't terribly obvious in advance. Can't think of any specific examples of where this use case is appropriate at the moment. It's a replacement to doing dummy_var = True, while dummy_var: and then set dummy_var = False when you want to dump out.

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

3 Comments

Thank you VERY much Casta! This was super helpful
Note that if you want to use if for some strange reason, you can trap it all in a while True loop. This will loop forever until you tell it to break. while True: if x < 9: x += 1 else: break. There's no reason to do that for this simplified example though
Thanks Adam, yeah that makes sense. I actually wanted to use a "for loop" instead of an "if" statement. I appreciate the input!
1

Control Statement like 'if' can be used for decision making and the statements under the condition runs only once. Instead you could make use of while loop for this purpose.

Comments

1

You need to also include a loop if you want something to repeat. The two simplest ways to do this are with a for or while loop.

for i in range(0,9):
 print "this works"

Or

x = 0
while(x < 9):
 print "this works"
 x = x + 1

1 Comment

You can leave out the 0 in your range function. range(9) will do the same. And the parentheses in the while loop are superfluous and impair readability: while x < 9: is enough.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.