1

I realize this question may be duplicated here and here, I can't seem to understand the basic principle behind where a while loop reads a variable and, when updating said variable, why it no longer refers to the updated variable.

I want to query for a number and, if a hidden number b is less than the input, add 1 and print the result until b is no longer less than the input.

In this example, if the input number is 5, I want the output to be

3
4
5

I know this can be done with a for loop, but I would like to iterate in more complicated ways on b when I don't know how many times I need to iterate. I have tried this with the if as a while as well, with similar (non)-results. Thanks in advance.

a=raw_input("Input number: ")
b=2
while True:
    if b<a:
        b+=1
        print b
    else:
        break

edit: Missed the 5 output, thanks.

2
  • 5
    It looks like you're inputting a string and treating it like a number. Perhaps you should try whatever you're doing with a = int(raw_input("Input number:")) Commented Jun 20, 2017 at 15:25
  • @kehlwood dear god. well, I'm an idiot. I'm coming from a Matlab and R background. always forget types. Thanks! Commented Jun 20, 2017 at 15:27

4 Answers 4

2

It looks like this is what you're asking for:

a=int(raw_input("Input number: "))
b=2

while b < a:
    b+=1
    print b

ETA: the condition for the 'while' loop will be checked after the nested code is executed. In this case, when 'b' is 4, 1 will be added to 'b' and it will print that value, but the loop will not start over because 'b' is no longer less than 'a'.

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

Comments

1

You code should work in this form, but the raw input function returns a string and not a number...

So you need to do this:

a=int(raw_input("Input number:"))

Comments

1
a=int(raw_input("Input number: "))
b=3
while True:
    if b<a:
        print b
        b+=1
    else:
        break

Your code is good! You just need to swap the print and the incrementation in the if statement to make it so that the variable is increased only after printing the number it's on. In this version, you need to start the program with b equalling 3!

Hope it helps! Cheers!

Comments

0

Your original example prints:

3
4
5

Explicit spec's require explicit code. In your code when the conditional is checked, var b is 4, then it increments and performs the print function without checking for the criteria you have spec'd.

You want to use the conditional i.e. b < a for the print function again.

e.g.

a=int(raw_input("Input number:"))
b=2
while True:
    if b < a:
        b += 1
    if b < a: 
        print b 
    else:
        break

FWIW, this hack gives a little insight into zero indexing

a=int(raw_input("Input number:"))
b=2
while True:
    if b+1 <a:
        print b+1
    else:
        break
    b += 1

Comments

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.