簡體   English   中英

預計 pyhton 中有一個縮進塊

[英]Expected an indented block in pyhton

我試着做一個簡單的警報。但是當我運行代碼時它顯示一個 IndentationError。代碼在這里。請給我一個答案。

import time
import winsound
print("Made by Ethan")


def myAlarm():
    try:
    myTime = list(map(int, input("Enter time in hr min sec\n") .split()))
    if len(mytime) == 3:
        total_secounds = myTime[0]*60*60+myTime[1]*60+myTime[2]
        time.sleep(total_secounds)
        frequency = 2500  
        duration = 10  
        winsound.Beep(frequency, duration)
    else:
        print("Please enter time in correct format as mentioned\n")
        myAlarm()
    exept Exception as e:
    print("This is the exception\n ", e, "So!, please enter correct details")
    myAlarm()


myAlarm()

Try/except 塊也需要在 Python 中縮進:

def myAlarm():
    try:
        myTime = list(map(int, input("Enter time in hr min sec\n") .split()))
        if len(mytime) == 3:
            total_secounds = myTime[0]*60*60+myTime[1]*60+myTime[2]
            time.sleep(total_secounds)
            frequency = 2500  
            duration = 10  
            winsound.Beep(frequency, duration)
        else:
            print("Please enter time in correct format as mentioned\n")
            myAlarm()
    except Exception as e:
        print("This is the exception\n ", e, "So!, please enter correct details")

myAlarm()

正如薄片所提到的,您的try塊和except塊需要縮進。 另一個錯誤是except拼寫錯誤為exept

import time
import winsound
print("Made by Ethan")


def myAlarm():
    try:
      myTime = list(map(int, input("Enter time in hr min sec\n") .split()))
      if len(mytime) == 3:
          total_secounds = myTime[0]*60*60+myTime[1]*60+myTime[2]
          time.sleep(total_secounds)
          frequency = 2500  
          duration = 10  
          winsound.Beep(frequency, duration)
      else:
          print("Please enter time in correct format as mentioned\n")
          myAlarm()
    except Exception as e:
      print("This is the exception\n ", e, "So!, please enter correct details")
      myAlarm()


myAlarm()

try-except縮進會產生問題:

def myAlarm():
    try:
        myTime = list(map(int, input("Enter time in hr min sec\n") .split()))
        if len(mytime) == 3:
            total_secounds = myTime[0]*60*60+myTime[1]*60+myTime[2]
            time.sleep(total_secounds)
            frequency = 2500  
            duration = 10  
            winsound.Beep(frequency, duration)
        else:
             print("Please enter time in correct format as mentioned\n")
             myAlarm()
    except Exception as e: # <--- spelling corrected from exept to except
        print("This is the exception\n ", e, "So!, please enter correct details")
        myAlarm()

您的代碼中有三個錯誤

  1. try-except 塊的縮進

  2. 除了的拼音

  3. 在一個地方你使用了 myTime,在另一個地方使用了 mytime。

import time
import winsound
print("Made by Ethan")


def myAlarm():
    try:
        myTime = list(map(int, input("Enter time in hr min sec\n") .split()))
        if len(myTime) == 3:
            total_secounds = myTime[0]*60*60+myTime[1]*60+myTime[2]
            time.sleep(total_secounds)
            frequency = 2500
            duration = 10
            winsound.Beep(frequency, duration)
        else:
            print("Please enter time in correct format as mentioned\n")
            myAlarm()
    except Exception as e:
        print("This is the exception\n ", e,
              "So!, please enter correct details")
        myAlarm()


myAlarm()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM