程序在 while 循环期间忽略 if 语句

时间:2021-03-01 21:00:26

标签: python python-3.x if-statement while-loop ignore

我正在编写一个使用给定方程更新坐标的 Python 程序:(x,y) = (ax-by, ay+bx)。这应该为每个步骤 (n) 完成,每个步骤 (c+1) 包括 import math import os import random import re import sys # # Complete the 'findPosition' function below. # # The function is expected to return an INTEGER_ARRAY. # The function accepts following parameters: # 1. INTEGER s # 2. INTEGER x0 # 3. INTEGER y0 # 4. INTEGER a # 5. INTEGER b # 6. INTEGER c # 7. INTEGER l # 8. INTEGER n # def findPosition(s, x0, y0, a, b, c, l, n): # Write your code here xf = yf = 0 t = 0 while (n > 0): while (t <= c): xf = (a * x0) - (b * y0) yf = (a * y0) + (b * x0) if (xf >= s): xf = xf - s if (yf >= s): yf = yf - s if (xf < 0): xf = xf + s if (yf < 0): yf = yf + s x0 = xf y0 = yf #print (xf, yf) t = t + 1 t = 0 n = n - 1 return (xf, yf) if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') first_multiple_input = input().rstrip().split() a = int(first_multiple_input[0]) b = int(first_multiple_input[1]) c = int(first_multiple_input[2]) l = int(first_multiple_input[3]) second_multiple_input = input().rstrip().split() s = int(second_multiple_input[0]) x0 = int(second_multiple_input[1]) y0 = int(second_multiple_input[2]) n = int(second_multiple_input[3]) result = findPosition(s, x0, y0, a, b, c, l, n) fptr.write(' '.join(map(str, result))) fptr.write('\n') fptr.close() 数量的微步骤。所以基本上对于 4 个步骤中的每一个,我都会更新位置 3 次。

if

问题是我的代码在程序中的某个时间(在步骤 3 的微步骤 1 期间)忽略了我的 (4, 7) (1, 18) (7 ,14) (0, 12) (11, 1) (21, 13) (6, 1) (11, 8) (14, 4) (1, 22) (3, 22) (7, 1) 语句。 我应该得到的输出是:

(4, 7)  (1, 18)  (7, 14)
(0, 12)  (11, 1)  (21, 13)
(6, 24)  (11, 31)  (14, 50)
(1, 91)  (-66, 160)  (-269, 231)

我实际得到的是:

a=2

这是用于输入:b=1c=2l=1s=23x0=3y0=2n=4def get_db(): """ This is my database engine to cloud firestore """ if 'db' not in g: cred = credentials.Certificate("key.json") firebase_admin.initialize_app(cred) g.db = firestore.client() return g.db def init_db(): """ Function that creates all collections """ db = get_db() for collection in collectionsName: db.collection(collection) @click.command("init-db") @with_appcontext def init_db_command(): """ I initialize db with command 'init-db' """ init_db() click.echo("base de datos inicializada") def init_app(app): """ run my flask app and connect to my cloud firestore db """ app.cli.add_command(init_db_command)

1 个答案:

答案 0 :(得分:0)

它不会忽略您的 if 语句。您的 if 语句没有反映您的需要。在第 6 步中,xf,yf 变为 (29,47)。 47 > 23,所以你减去 23 得到 24。你不允许 xf 或 yf 大于 TWICE 的情况。你想要的是模运算符,如下所示。哦,请注意,在 Python 中,ifwhilereturn 语句不需要括号。

def findPosition(s, x0, y0, a, b, c, l, n):
    for i in range(n):
        for t in range(c+1):
            xf = ((a * x0) - (b * y0)) % s
            yf = ((a * y0) + (b * x0)) % s
            x0 = xf
            y0 = yf
            print (i, t, c, s, xf, yf)
    return xf, yf