Home > OS >  How to add an exact amount to the last value given in a loop?
How to add an exact amount to the last value given in a loop?

Time:10-17

I'm making an Illustrator bot to do an specific task, in this case to fill a letter size sheet with squares (the user inputs the H & W of the square). For example if I input a 2 x 3 rectangle, the width is 2 inches, the program adds the width of the rectangle (2 inches), constantly by itself and stops just before reaching the limit.

Example: if the width is 2, it will add 2 2 2 = 8, it stops in 8 because if it adds 2 more the result will be 10 (Exceeds the limit of 8.5)

My issue is that I want to perform a task after the loop finishes, using the last value given in the loop (8.0), and I don't know how.

For example: if the last value given in the loop was 8.0, I want to add 0.25 to it (keep in mind that the value will not always be the same). How can I add always the same amount (0.25), no matter what is the last value given in the loop?

My code:

w = float(input("insert width: "))
h = float(input("insert height: "))

letter_w = 8.5
letter_h = 11

addition = (w   w)
while addition < letter_w:
    print(addition)
    print("under the limit")
    addition = addition   w

Result:

insert width: 2
insert height: 2
4.0
under the limit
6.0
under the limit
8.0
under the limit

Process finished with exit code 0

CodePudding user response:

I think the key is that you need to store the value that you want to end up with in a variable so that you can do things with it after you are finished looping. Maybe you can try something like this:

w = float(input("insert width: "))
h = float(input("insert height: "))

letter_w = 8.5
letter_h = 11

added_w = w
while (added_w   w) < letter_w:
    added_w  = w
    print(added_w)
    print("under the limit")
    
print("Final size:")
print(added_w   0.25)

Which, following your example should yield:

insert width: 2
insert height: 2
4.0
under the limit
6.0
under the limit
8.0
under the limit
Final size:
8.25
  • Related