Python:退出循环? [英] Python : Exiting for loop?
问题描述
pre $
从字符串import ascii_lowercase,ascii_uppercase
def creer_encodeur_cesar(距离):
retour = lambda x:encodeur_cesar(x,distance)
return retour
$ b def encodeur_cesar(string,distance):
tabLowerCase = list(ascii_lowercase)
tabUpperCase = list(ascii_uppercase)
tabString = list(string)
$ b l_encodedStr = []
为tabString中的字符:
position = 0
if char ==:
pass
elif char.isupper():
#do something
$ b $ elif char.islower()
for ctl in range(0,len(tabLowerCase)):
position = ctl
if char == tabLowerCase [ctl]:
if(ctl + distance)> 26:
位置= ctl +距离 - 25
char = tabLowerCase [位置+距离]
l_encodedStr.append(char)
#如何突破这里?
encodedStr = str(l_encodedStr)
return encodedStr
encodeur5 = creer_encodeur_cesar(5)
print(encodeur5 lionel))
所以,在我的第二条ELIF语句中,找到并加密一个字符,而不是循环遍历整个字母表。我曾尝试使用 break
,但是它已经打破了主循环。不是我想要的。我看到我可以用 try
,除了
和 raise
,但是我不知道如何做到这一点,这是一个好主意?
最好的方法是什么?什么是这种情况下的良好做法?
任何帮助,将不胜感激,并提前感谢!
您可以使用 continue
关键字。
来自文件:
>>> (2,10):
... if num%2 == 0:
... print找到一个偶数,num
... continue
...打印找到一个数字,num
找到一个偶数2
找到一个数字3
找到一个偶数4
找到一个数字5
找到一个偶数6
找到一个数字7
找到一个偶数8
找到一个数字9
I did some research on SO and am aware that many similar questions have been asked but I couldn't quite get my anwser. Anyway, I'm trying to build a library to "encrypt" a string with "Cesar's number" technique wich means I have to take the string and replace each letters with another letter X positions away in the alphabet (I hope that makes sense). Here's my code :
from string import ascii_lowercase, ascii_uppercase
def creer_encodeur_cesar(distance):
retour = lambda x: encodeur_cesar(x, distance)
return retour
def encodeur_cesar(string, distance):
tabLowerCase = list(ascii_lowercase)
tabUpperCase = list(ascii_uppercase)
tabString = list(string)
l_encodedStr = []
for char in tabString:
position = 0
if char == " ":
pass
elif char.isupper():
#do something
elif char.islower():
for ctl in range(0, len(tabLowerCase)):
position = ctl
if char == tabLowerCase[ctl]:
if (ctl + distance) > 26:
position = ctl + distance - 25
char = tabLowerCase[position + distance]
l_encodedStr.append(char)
#How to break out of here??
encodedStr = str(l_encodedStr)
return encodedStr
encodeur5 = creer_encodeur_cesar(5)
print(encodeur5("lionel"))
So, in my second elif statement, I want to break once I have succesfully found and encrypted a character instead of looping trough the whole alphabet. I have tried to use break
but it broke out of the main for loop. Not what I want. I saw that I could use try
except
and raise
but I don't quite know how I could do that and is it a good idea?
What's the best way to do this? What are the good practices in this case?
Any help would be appreciated and thanks in advance!
You can use the continue
keyword.
From the docs:
>>> for num in range(2, 10):
... if num % 2 == 0:
... print "Found an even number", num
... continue
... print "Found a number", num
Found an even number 2
Found a number 3
Found an even number 4
Found a number 5
Found an even number 6
Found a number 7
Found an even number 8
Found a number 9
这篇关于Python:退出循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!