In the code below you'll see it asking for a 'shift' value. My problem is that I want to limit the input to 1 through 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
How do I do this easily?
Try something like this
Could put in while loop but you should limit user inputs so it doesn't become infinite
Use an if-condition:
Or a while loop with the if-condition:
Use a
while
loop to keep asking them for input until you receive something you consider valid:You'll also want to have a
try-except
block around theint()
call, in case you get aValueError
(if they typea
for example).Note if you use Python 2.x, you'll want to use
raw_input()
instead ofinput()
. The latter will attempt to interpret the input as Python code - that can potentially be very bad.Another implementation: