0

I want my script to repeat input question until the user prompt the right answer. After the user prompted the right answer, the script has to go on with the relative if statement In this case hostname or file. I came out with the below code, however seems to fall into infinite loop.

import socket

def ipFromHost():
  opt1 = input('Do you want provide  hostname or file: ') 
  while opt1 != 'hostname' or 'file':
    input('Please, type "hostname" or "file"')
  if opt1 == 'hostname':
    optHostname = input('Please, provide hostname: ')
    print(socket.gethostbyname(optHostname.strip()))
  elif opt1 == 'file':
    optFile = input('Please, provide file name: ')
    with open(optFile) as inputFile:
      for i in inputFile:
        print(socket.gethostbyname(i.strip()))

Thanks!

  • 2
    `while opt1 != 'hostname' or 'file':` is not doing what you think it is... – DavidG Feb 22 '18 at 15:51
  • 1
    `while opt1 != 'hostname' or 'file':` This is interpretead as : While opt1 != 'hostname' or 'file' == true Or, 'file' == true In fact, every other thing than 0 is true – Thomas Grockowiak Feb 22 '18 at 15:54

2 Answers2

1

You have an infinite loop because the condition while opt1 != 'hostname' or 'file': checks 2 conditions:

  1. opt1 != 'hostname' OR
  2. 'file'

even if opt1 != 'hostname' will evaluate to True, the second condition actually checks if 'file' is True or False (compare if opt1 != 'file' to if 'file'). You can check this answer about Boolean value of strings in python

Since if 'file' is always True you gen an infinite loop

Fix with

  1. while opt1 != 'hostname' and opt1 != 'file': [using AND since opt1 must be different from both options] OR
  2. while opt1 not in ('hostname', 'file') [which seems neater in my opnion]

Your code should look something like this:

def ipFromHost():
  opt1 = input('Do you want provide  hostname or file: ') 
  while opt1 not in ('hostname', 'file'):
    opt1 = input('Please, type "hostname" or "file"')
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • Thanks for your answer and explanation. I can see now the loop is "fixed" however script in not able to move on if statement after prompt hostname or file `>>> ipFromHost() Do you want provide hostname or file: home Please, type "hostname" or "file": home Please, type "hostname" or "file": file Please, type "hostname" or "file": hostname Please, type "hostname" or "file": ` –  Feb 22 '18 at 16:04
  • @FedericoOlivieri see my answer: your code does not update the value of opt1 with the user input. – tehhowch Feb 22 '18 at 16:08
  • @tehhowch I don't understand what you mean. Could you please provide an example to how fix it so I can maybe understand. Thanks! –  Feb 22 '18 at 16:16
  • 1
    @FedericoOlivieri - as @tehhowch said, while inside the loop, you ask for `input()` but you don't assign the result into `opt1` – CIsForCookies Feb 22 '18 at 16:16
  • @FedericoOlivieri I did provide an example. See the other answer to your question, and compare the two lines I wrote with the relevant lines you originally wrote. – tehhowch Feb 22 '18 at 16:19
  • 1
    Sorry guys, I have got it now. It works as expected. Thanyou for your time –  Feb 22 '18 at 16:24
0
while opt1 not in ["hostname", "file"]:
    opt1 = input(...

As mentioned in comments, your while loop has bad conditions.

tehhowch
  • 9,645
  • 4
  • 24
  • 42
  • 2
    A tuple (instead of a list) is more performant and better reflects the immutability of the comparison values. – guidot Feb 22 '18 at 16:02