Search code examples
pythonwhile-loopindentation

Indent error is being triggered and I cannot find a solution


Code:

def funt():
  print(Fore.GREEN, end='')
  tool = input('Enter Desired Tool: ')
  if tool == 'web':
      try:
        print(Fore.CYAN, end='')
        site = input('Please Enter The Website Here: ')
        response = requests.get(site)
        requests.get(site)
        if response.status_code == 200:
          print(f'{Fore.GREEN}Online!')
          sleep(1)
        else:
          print(f'{Fore.RED}Offline!')
          sleep(1)

while True:
    funt()

Location of Error is while True: . The Error Is As Follows:

    while True:
    ^
IndentationError: unexpected unindent

I cannot find a solution, there is not any sign of indentation in the while loop.


Solution

  • try expects an except block followed by it.

    You can modify your code as follows:

    def funt():
      print(Fore.GREEN, end='')
      tool = input('Enter Desired Tool: ')
      if tool == 'web':
          try:
            print(Fore.CYAN, end='')
            site = input('Please Enter The Website Here: ')
            response = requests.get(site)
            requests.get(site)
            if response.status_code == 200:
              print(f'{Fore.GREEN}Online!')
              sleep(1)
            else:
              print(f'{Fore.RED}Offline!')
              sleep(1)
          except:
            pass
    
    while True:
        funt()
    

    But writing proper code requires you to handle exceptions. So, if possible write a piece of code in the except block.