Ograniczanie danych wprowadzanych przez użytkownika do zakresu w Pythonie
W poniższym kodzie zobaczysz pytanie o wartość „shift”. Mój problem polega na tym, że chcę ograniczyć wejście do 1 do 26.
For char in sentence:
if char in validLetters or char in space: #checks for
newString += char #useable characters
shift = input("Please enter your shift (1 - 26) : ")#choose a shift
resulta = []
for ch in newString:
x = ord(ch) #determines placement in ASCII code
x = x+shift #applies the shift from the Cipher
resulta.append(chr(x if 97 <= x <= 122 else 96+x%122) if ch != \
' ' else ch) # This line finds the character by its ASCII code
Jak to zrobić łatwo?