you have to assign choice
new value inside inner while loop. this way, new input is considered before iterating again and again.
this should do the trick:
while choice < 1 and choice > 5:
choice = int(input("Invalid choice, please enter either 1, 2, 3, 4 or 5."))
A random advice: If you want to keep dataset strictly equal to [1,2,3,4,5], you should avoid choice<1
and choice>5
. This makes your program vulnerable to invalid inputs such as 3.5
.
Instead, you should do this:
valid_inputs = [1, 2, 3, 4, 5]
choice = int(input("Pick a number from [1, 2, 3, 4, 5]:"))
while choice not in valid_inputs:
choice = int(input("Invalid choice, please enter either 1, 2, 3, 4 or 5."))
<rest of stuff here>
What I understood from your comments is that, in get_offset()
, you want user to type a value between 1 and 94. If user enters something out of that range, you want to ask question again and again. The code below should do what you want:
def get_offset():
# first, ask for a value.
offset = int(input("Enter a value between 1 and 94:"))
while offset < 1 and offset > 94:
# if the value is invalid, trap user inside this while loop.
# user will be stuck here (while loop will return true)
# until a valid input is received.
offset = int(input("Invalid input. Please enter a value between 1 and 94:"))
# if we proceed down here, it means we are over while loop
# and it implies that user has given us valid input. return value.
return offset
Secondly, inside main while loop, you can call get_offset()
like this:
if choice == 1:
string_input = input("Please enter string to encrypt: ")
input_offset = get_offset()