我写的行代码被标记为“ unindent不匹配任何外部缩进级别”,这是为什么?

问题描述 投票:0回答:2

下面的代码块用于上下文。

import requests
from termcolor import colored
import time
import replit
from time import sleep
import random
from random import seed
from random import randint
import sys

task1 = 0
task2 = 0
task3 = 0
task4 = 0
task5 = 0
task6 = 0
task7 = 0
task8 = 0
task9 = 0
task10 = 0
logout = 0

def mainterminal():
  print(colored("\n 1) Tasks \n 2) Device Management \n 3) Creature Information \n 4) Logoff", "white"))
  mtchoice = input()
  if mtchoice == "1":
   taskterminal()

def taskterminal():
  global task1
  global task2
  global task3
  global task4
  global task5
  global task6
  global task7
  global task8
  global task9
  global task10
  global logout
  print(colored("\n 1) Print Posters \n 2) Print Flyers \n 3) Wash Dishes \n 4) Clean Tables \n 5) Analyze Daily Revenue \n 6) Create New Playlist \n 7) Purchase Balloons \n 8) Purchase Paper Cups \n 9) Clean Stage \n 10) Refill Coffee \n 11) Return to main terminal", "white"))
  tchoice = input()
  if tchoice == "1":
   if task1 < 1:
    typer(colored("Printing Posters.", "blue"))
    print("")
    typer(colored("..........", "blue"))
    task1 = 0 + 1
    logout = 0 + 0.1 
    print(task1)
    print(logout)
    taskterminal()
  else:
   return(colored("Task is already completed", "red"))
   taskterminal()

 elif tchoice == "2":
   if task2 < 2:
    typer(colored("Printing Flyers.", "blue"))
    print("")
    typer(colored("..........", "blue"))
    task2 = 0 + 1
    logout = 0 + 0.1 
    print(task2)
    print(logout)
    taskterminal()
  else:
   return(colored("Task is already completed", "red"))
   taskterminal()
 elif tchoice == "11":
   replit.clear()
   mainterminal()


def typer(words):
  for char in words:
        time.sleep(0.05)
        sys.stdout.write(char)
        sys.stdout.flush()

def typertask(words):
  for char in words:
        time.sleep(2)
        sys.stdout.write(char)
        sys.stdout.flush()

def typerslow(words):
  for char in words:
        time.sleep(1.5)
        sys.stdout.write(char)
        sys.stdout.flush()

这应该可以,但是当我运行它时,出现此错误代码

    elif tchoice == "2":
                       ^
IndentationError: unindent does not match any outer indentation level

if tchoice ==“ 1”:的部分没有给出错误代码,所以我不明白为什么将其更改为elif tchoice ==“ 2”:时会给出错误代码。

帮助?

如果您需要帮助,我会提供更多代码。

python menu definition
2个回答
1
投票

缩进在代码的许多部分中不正确:

import requests
from termcolor import colored
import time
import replit
from time import sleep
import random
from random import seed
from random import randint
import sys

task1 = 0
task2 = 0
task3 = 0
task4 = 0
task5 = 0
task6 = 0
task7 = 0
task8 = 0
task9 = 0
task10 = 0
logout = 0

def mainterminal():
    print(colored("\n 1) Tasks \n 2) Device Management \n 3) Creature Information \n 4) Logoff", "white"))
    mtchoice = input()
    if mtchoice == "1":
        taskterminal()

def taskterminal():
    global task1
    global task2
    global task3
    global task4
    global task5
    global task6
    global task7
    global task8
    global task9
    global task10
    global logout
    print(colored("\n 1) Print Posters \n 2) Print Flyers \n 3) Wash Dishes \n 4) Clean Tables \n 5) Analyze Daily Revenue \n 6) Create New Playlist \n 7) Purchase Balloons \n 8) Purchase Paper Cups \n 9) Clean Stage \n 10) Refill Coffee \n 11) Return to main terminal", "white"))
    tchoice = input()
    if tchoice == "1":
        if task1 < 1:
            typer(colored("Printing Posters.", "blue"))
            print("")
            typer(colored("..........", "blue"))
            task1 = 0 + 1
            logout = 0 + 0.1 
            print(task1)
            print(logout)
            taskterminal()
        else:
            return(colored("Task is already completed", "red"))
            taskterminal()

    elif tchoice == "2":
        if task2 < 2:
            typer(colored("Printing Flyers.", "blue"))
            print("")
            typer(colored("..........", "blue"))
            task2 = 0 + 1
            logout = 0 + 0.1 
            print(task2)
            print(logout)
            taskterminal()
        else:
            return(colored("Task is already completed", "red"))
            taskterminal()

    elif tchoice == "11":
        replit.clear()
        mainterminal()


def typer(words):
    for char in words:
        time.sleep(0.05)
        sys.stdout.write(char)
        sys.stdout.flush()

def typertask(words):
    for char in words:
        time.sleep(2)
        sys.stdout.write(char)
        sys.stdout.flush()

def typerslow(words):
    for char in words:
        time.sleep(1.5)
        sys.stdout.write(char)
        sys.stdout.flush()

尝试一下,让我知道是否有问题。我的建议是使用文本编辑器或jupyter笔记本,这样可以更轻松地解决与语法有关的问题。


0
投票

该代码存在缩进问题,我发现您在块中混入了空格,如果对其进行纠正,可以在我的IDE上正常工作(pyCharm),您还可以使用Sublime Text 3,它是有用的工具来检查您的缩进,您也可以使用“视图”>“缩进”>“使用空格的意图”。

import requests
from termcolor import colored
import time
import replit
from time import sleep
import random
from random import seed
from random import randint
import sys

task1 = 0
task2 = 0
task3 = 0
task4 = 0
task5 = 0
task6 = 0
task7 = 0
task8 = 0
task9 = 0
task10 = 0
logout = 0

def mainterminal():
  print(colored("\n 1) Tasks \n 2) Device Management \n 3) Creature Information \n 4) Logoff", "white"))
  mtchoice = input()
  if mtchoice == "1":
   taskterminal()

def taskterminal():
  global task1
  global task2
  global task3
  global task4
  global task5
  global task6
  global task7
  global task8
  global task9
  global task10
  global logout
  print(colored("\n 1) Print Posters \n 2) Print Flyers \n 3) Wash Dishes \n 4) Clean Tables \n 5) Analyze Daily Revenue \n 6) Create New Playlist \n 7) Purchase Balloons \n 8) Purchase Paper Cups \n 9) Clean Stage \n 10) Refill Coffee \n 11) Return to main terminal", "white"))
  tchoice = input()
  if tchoice == "1":
   if task1 < 1:
    typer(colored("Printing Posters.", "blue"))
    print("")
    typer(colored("..........", "blue"))
    task1 = 0 + 1
    logout = 0 + 0.1
    print(task1)
    print(logout)
    taskterminal()
   else:
    return colored("Task is already completed", "red")
    taskterminal()
  elif tchoice == "2":
   if task2 < 2:
    typer(colored("Printing Flyers.", "blue"))
    print("")
    typer(colored("..........", "blue"))
    task2 = 0 + 1
    logout = 0 + 0.1
    print(task2)
    print(logout)
    taskterminal()
   else:
    return(colored("Task is already completed", "red"))
    taskterminal()
  elif tchoice == "11":
   replit.clear()
   mainterminal()


def typer(words):
  for char in words:
        time.sleep(0.05)
        sys.stdout.write(char)
        sys.stdout.flush()

def typertask(words):
  for char in words:
        time.sleep(2)
        sys.stdout.write(char)
        sys.stdout.flush()

def typerslow(words):
  for char in words:
        time.sleep(1.5)
        sys.stdout.write(char)
        sys.stdout.flush()
© www.soinside.com 2019 - 2024. All rights reserved.