我正在制作一個 Illustrator 機器人來執行特定任務,在這種情況下,用正方形填充字母大小的表格(用戶輸入正方形的 H 和 W)。例如,如果我輸入一個 2 x 3 的矩形,寬度為 2 英寸,程式會不斷增加矩形的寬度(2 英寸),并在達到限制之前停止。
示例:如果寬度為 2,它將添加 2 2 2 = 8,它會在 8 處停止,因為如果再添加 2 結果將是 10(超過 8.5 的限制)
我的問題是我想在回圈完成后使用回圈 (8.0) 中給定的最后一個值執行任務,但我不知道如何執行。
例如:如果回圈中給出的最后一個值是 8.0,我想給它加上 0.25(請記住,該值并不總是相同的)。無論回圈中給出的最后一個值是什么,我如何始終添加相同的數量(0.25)?
我的代碼:
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
結果:
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
uj5u.com熱心網友回復:
我認為關鍵是你需要將你想要結束的值存盤在一個變數中,這樣你就可以在完成回圈后用它做一些事情。也許你可以嘗試這樣的事情:
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)
其中,按照您的示例應該會產生:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/322610.html
