Python 应为pyhton中的缩进块
我试图发出一个简单的警报。但是当我运行代码时,它显示一个缩进错误。代码在这里。请给我一个答案Python 应为pyhton中的缩进块,python,Python,我试图发出一个简单的警报。但是当我运行代码时,它显示一个缩进错误。代码在这里。请给我一个答案 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 = my
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缩进会产生问题:
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()
您的代码中有三个错误 试块缩进 except的拼写 在一个地方你使用了myTime,在另一个地方你使用了myTime
Try/except块也需要在Python中缩进: def myAlarm: 尝试: myTime=listmapint,输入时间(以小时-分钟-秒为单位)\n.拆分 如果lenmytime==3: 总数=我的时间[0]*60*60+myTime[1]*60+myTime[2] time.sleeptotal_seconds 频率=2500 持续时间=10 winsound.蜂鸣音频率、持续时间 其他: 打印请按上述正确格式输入时间\n 我的闹钟 例外情况除外,如e: print这是个例外\n,e,So!,请输入正确的详细信息 我的闹钟 正如flakes提到的,try块和except块需要缩进。 另一个错误是except拼写错误
试一下块,除了块也需要缩进。为什么你认为你会得到缩进错误?您自己已经尝试过解决这个问题了吗?您知道缩进在Python中很重要吗?顺便说一句,你拼错了,除了。顺便说一句,欢迎来到SO!查看和。
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()
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()