I'm working on an experimental project (creating a basic enigma machine mechanism), and I'm running into an issue when reassigning a variable. I have a "check" function that checks to ensure that the value entered by the user is a number between 1 and 26, if it isn't, it calls the input function again, asking for a new value. Everything works fine if I input a number between 1 and 26 (ex: 4), however, if I enter 29 or "qwerty", it begins to mess up. It calls the input function again (as expected), and if I were to then enter 4 as the value, the variable gets assigned as "None".
How can I fix this?
CLI output of it when working (ex: entered 4):
What is the message you would like to encrypt?as
For the next 3 questions, please input ONLY a number from 1 to 26
What is the first key value?4
4
CLI output of it when failing (ex: entered 28 then 4):
What is the message you would like to encrypt?asd
For the next 3 questions, please input ONLY a number from 1 to 26
What is the first key value?28
You must input a number between 1 and 26!
What is the first key value?4
None
Code:
class Input:
def message():
msg = input("What is the message you would like to encrypt?")
msg = msg.upper()
print("\n For the next 3 questions, please input ONLY a number from 1 to 26")
def check(input):
try:
if int(input) < 1 or int(input) > 26:
return False
else:
return True
except:
return False
def getKey(keyNum):
word = ""
if keyNum == 1:
word = "first"
elif keyNum == 2:
word = "second"
else:
word = "third"
s = input("What is the {} key value?".format(word))
chk = Input.check(s)
if chk:
return(s)
else:
print("You must input a number between 1 and 26!")
Input.getKey(1)
inp = Input
inp.message()
s1 = inp.getKey(1)
print(s1)