[英]How to write code which return to the first step in python
輸入:
val=input('Do you want to encrypt or decrypt a message or quit: ')
if val=="encrypt":
enr=input('Please enter a string: ')
for e in enr:
if "a"<=e<="z":
print(chr(ord("a")+(ord(e)-ord("a")+3)%26),end="")
elif "A"<=e<="Z":
print(chr(ord("A")+(ord(e)-ord("A")+3)%26),end="")
else:
print(e,end="")
if val=="decrypt":
dec=input('Please enter a string: ')
for d in dec:
if "a"<=d<="z":
print(chr(ord("a")+(ord(d)-ord("a")-3)%26),end="")
elif "A"<=d<="Z":
print(chr(ord("A")+(ord(d)-ord("A")-3)%26),end="")
else:
print(d,end="")
if val=="quit":
print('Thank you for using')
打印出加密或解密消息后,go 回到第一個輸入問題:val=input('Do you want to encrypt or decrypt a message or quit: ') if quit then stop the loop
將您的加密/解密代碼放入 function; 然后你可以很容易地在一個循環中調用它,當它退出時你會break
它。
def shift_chars(msg: str, shift: int) -> str:
def shift_char(c: str) -> str:
if "a" <= c <= "z":
return chr(ord("a") + (ord(c) - ord("a") + shift) % 26)
elif "A" <= c <= "Z":
return chr(ord("A") + (ord(c) - ord("A") + shift) % 26)
else:
return c
return ''.join(shift_char(c) for c in msg)
while True:
val = input('Do you want to encrypt or decrypt a message or quit: ')
if val == "encrypt":
shift = 3
elif val == "decrypt":
shift = -3
elif val == "quit":
print('Thank you for using')
break
else:
continue
print(shift_chars(input('Please enter a string: '), shift))
這種方法應該有助於:
我們首先使用您的input()
function 收集用戶的輸入。
然后我們將其他代碼放入一個while
循環中,該循環在每個循環中檢查 val 的值。 此外,我們在 while 循環的末尾添加一行,詢問用戶是否要加密/解密或退出。 如有必要,這會更新val
的當前值以觸發while
循環停止:
val=input('Do you want to encrypt or decrypt a message or quit: ')
while val != 'quit':
if val=="encrypt":
enr=input('Please enter a string: ')
for e in enr:
if "a"<=e<="z":
print(chr(ord("a")+(ord(e)-ord("a")+3)%26),end="")
elif "A"<=e<="Z":
print(chr(ord("A")+(ord(e)-ord("A")+3)%26),end="")
else:
print(e,end="")
if val=="decrypt":
dec=input('Please enter a string: ')
for d in dec:
if "a"<=d<="z":
print(chr(ord("a")+(ord(d)-ord("a")-3)%26),end="")
elif "A"<=d<="Z":
print(chr(ord("A")+(ord(d)-ord("A")-3)%26),end="")
else:
print(d,end="")
val=input('Do you want to encrypt or decrypt a message or quit: ')
print('Thank you for using')
正如一些評論/答案中所指出的,將一些代碼封裝到 function 中可能會很有用,以使while
循環更清晰一些。
下面是一種執行此操作的方法,它與您的原始代碼相當真實。 我們添加一個加密器/解密器 function 以便我們可以減少一些重復代碼。 我們確定班次應該向上還是向下 3 或 -3。 然后我們使用那個shift_value
來改變字符的值。
def enc_decryptor(transformation):
# Determine whether to shift up OR down
if transformation == 'encrypt':
shift_value = 3
elif transformation == 'decrypt':
shift_value = -3
# Perform the transformation
msg=input('Please enter a string: ')
for char in msg:
if "a" <= char <= "z":
print(chr(ord("a")+(ord(char)-ord("a") + shift_value)%26),end="")
elif "A" <= char <= "Z":
print(chr(ord("A")+(ord(char)-ord("A") + shift_value)%26),end="")
else:
print(char, end="")
我們需要修改我們的while
循環以說明新代碼,我們可以看到 while 循環現在更簡單了。
val=input('Do you want to encrypt or decrypt a message or quit: ')
while val != 'quit':
if val=="encrypt":
enc_decryptor(transformation=val)
if val=="decrypt":
enc_decryptor(transformation=val)
val=input('Do you want to encrypt or decrypt a message or quit: ')
print('Thank you for using')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.