-1

need some help in the following code as it goes into infinite loop and does not validate user input: the get_offset is the function. Just edited need some help with the encryption part to be done in a function defined

def get_offset(offset):
    while True:
        value = (offset)
        if value < 1:
            print("")
        if value > 94:
            print("")
    return offset

my_details = display_details()

get_choice = get_menu_choice()

print(my_details)
print(get_choice)

count = 10

while count > 0:

    count -=1

    encrypted = ""

    choice = int(input("What would you like to do [1,2,3,4,5]: "))

    while choice <1 and choice > 5:
        print("Invalid choice, please enter either 1, 2, 3, 4 or 5.")

    if choice == 1:
        string_input = input("Please enter string to encrypt: ")
        input_offset = get_offset(int(input("Please enter offset value (1 to 94): ")))

   **for letter in string_input:
        x = ord(letter)
        encrypted += chr(x + input_offset)
        if x < 32:
            x += 94
        if x > 126:
            x -= 94

    print(encrypted)**
Jay Sadat
  • 11
  • 4
  • in your first while loop, you never change "offset" so it is working on the same thing every time ==> infinite loop – user1443098 Apr 26 '18 at 13:37
  • so what needs editing Ali? I am a beginner – Jay Sadat Apr 26 '18 at 13:39
  • @JaySadat I edited your post for readability. your code needed indentation for markdown support, so that readers can distinguish what you ask and what your code is. – Ali Yılmaz Apr 26 '18 at 13:41
  • JaySadat: **it is completely unacceptable to reask the same question twice in a row**: you asked previously on [Apr 23](https://stackoverflow.com/questions/49974356/file-decryption-with-ascii/), saying *"Please help me out. I need a quick reply. I would really appreciate it?", I answered. All you did was complain about the answer. Then you reasked yesterday. This sort of behavior gets you banned on SO. – smci Apr 28 '18 at 00:44

1 Answers1

0

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()
Ali Yılmaz
  • 1,657
  • 1
  • 11
  • 28
  • I am asking about the get_offset in the choice == 1 section – Jay Sadat Apr 26 '18 at 13:44
  • oh well, I got it wrong. But still, you will be stuck inside that while loop, once you enter an invalid input. – Ali Yılmaz Apr 26 '18 at 13:46
  • what do you want to do in `get_offset()`? – Ali Yılmaz Apr 26 '18 at 13:49
  • i want to limit the user offset input between 1 and 94. as you see i created a function and used it in the offset input variable but it still does not work – Jay Sadat Apr 26 '18 at 13:51
  • Can you please be more clear on what you want to do? What do you mean by user offset input? Can you specify your goal with example input and output? – Ali Yılmaz Apr 26 '18 at 13:53
  • input_offset = get_offset(int(input("Please enter offset value (1 to 94): "))) – Jay Sadat Apr 26 '18 at 13:56
  • in the above statement the input must be between 1 and 94 and i need to accomplish that with a function defined as get_offset() as you can see in the code and there is no output displayed, just the input is repeated until a value between 1 and 94 is entered – Jay Sadat Apr 26 '18 at 13:57
  • I updated the answer for get_offset. I hope I got it right. – Ali Yılmaz Apr 26 '18 at 14:08
  • for letter in string_input: x = ord(letter) encrypted += chr(x + input_offset) if x < 32: x += 94 if x > 126: x -= 94 print(encrypted) – Jay Sadat Apr 26 '18 at 14:29
  • Ali Yilmaz brother could you help me out in this final section. I would really appreciate it – Jay Sadat Apr 26 '18 at 14:36