如何在 python 3 的 while 循环中嵌套 if 语句
How to nest if statement in a while loop in python 3
我在使用以下代码时遇到问题
try:
while True:
# Get next line from file
line = fileHandler.readline()
plaintext = pow(int(line), a, n) # a, n and line read from file are integers.
if len(str(plaintext)) % 2 != 0:
plaintext = '0{}'.format(plaintext) # adding zero if odd no of digits
i = 0
while i < len(plaintext) - 1:
print(plaintext)
row = int(plaintext[i])
col = int(plaintext[i + 1])
decrypted.append(matrix[row][col])
if row > 0:
row -= 1
print(matrix[row][col - 1])
i = i + 2
print(plaintext)
if not line:
print(decrypted)
break
except ValueError:
pass
当 len(plaintext) % 2 != 0 失败时,下面的 while 循环中的代码将无法执行。我知道我在 if 循环中有 while 循环所以它发生了。但是当我将 while 循环移出 if 时,什么也没有执行。有人可以告诉我我做错了什么。
我现在得到以下输出:
19475276822512119441620228045431117884359382536936226816
05297161768145254779131968343762551184670149997720486635
H
05297161768145254779131968343762551184670149997720486635
p
05297161768145254779131968343762551184670149997720486635
&
第一行的操作没有执行,但是在if
时执行
len(str(plaintext)) % 2 != 0:
plaintext = '0{}'.format(plaintext)
这个条件成立。
希望我说清楚了。谢谢!
When len(plaintext) % 2 != 0 fails the code in the while loop below it fails to execute
是的,这就是 if 语句的工作原理
when I move the while loop out of if, nothing is execute
不清楚你把它移到外面多远,但它仍然需要在外部 while 循环内,你可以使用一个范围而不是另一个 while 循环
while True:
...
plaintext = str(pow(int(line), a, n))
if len(plaintext) % 2 != 0:
plaintext = '0{}'.format(plaintext) # adding zero if odd no of digits
# indent if you only want to execute this for odd length numbers
for i in range(0, len(plaintext), 2):
print(plaintext)
# ...
我在使用以下代码时遇到问题
try:
while True:
# Get next line from file
line = fileHandler.readline()
plaintext = pow(int(line), a, n) # a, n and line read from file are integers.
if len(str(plaintext)) % 2 != 0:
plaintext = '0{}'.format(plaintext) # adding zero if odd no of digits
i = 0
while i < len(plaintext) - 1:
print(plaintext)
row = int(plaintext[i])
col = int(plaintext[i + 1])
decrypted.append(matrix[row][col])
if row > 0:
row -= 1
print(matrix[row][col - 1])
i = i + 2
print(plaintext)
if not line:
print(decrypted)
break
except ValueError:
pass
当 len(plaintext) % 2 != 0 失败时,下面的 while 循环中的代码将无法执行。我知道我在 if 循环中有 while 循环所以它发生了。但是当我将 while 循环移出 if 时,什么也没有执行。有人可以告诉我我做错了什么。
我现在得到以下输出:
19475276822512119441620228045431117884359382536936226816 05297161768145254779131968343762551184670149997720486635
H
05297161768145254779131968343762551184670149997720486635
p
05297161768145254779131968343762551184670149997720486635
&
第一行的操作没有执行,但是在if
时执行len(str(plaintext)) % 2 != 0:
plaintext = '0{}'.format(plaintext)
这个条件成立。
希望我说清楚了。谢谢!
When len(plaintext) % 2 != 0 fails the code in the while loop below it fails to execute
是的,这就是 if 语句的工作原理
when I move the while loop out of if, nothing is execute
不清楚你把它移到外面多远,但它仍然需要在外部 while 循环内,你可以使用一个范围而不是另一个 while 循环
while True:
...
plaintext = str(pow(int(line), a, n))
if len(plaintext) % 2 != 0:
plaintext = '0{}'.format(plaintext) # adding zero if odd no of digits
# indent if you only want to execute this for odd length numbers
for i in range(0, len(plaintext), 2):
print(plaintext)
# ...