raspberry pi项目的Python闹钟代码问题

2024-03-29 06:42:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我为我正在工作的一个项目(咖啡酿造闹钟)的闹钟部分编写了这段代码。当我运行程序时,它要么跳到“yikes…”部分,要么返回错误

AttributeError: 'str' object has no attribute 'start'

有人知道如何修理这个闹钟吗?我只需要一双新的眼睛,因为我对python还不熟悉,而且已经花了很长时间研究代码了。在

import time
import os
import threading


class Alarm(threading.Thread):
    def __init__(self, hours, minutes):
        super(Alarm, self).__init__()
        self.hours = int(hours)
        self.minutes = int(minutes)
        self.keep_running = True

    def run(self):
        try:
            while self.keep_running:
                now = time.localtime()
                if (now.tm_hour == self.hours and now.tm_min == self.minutes):
                    print("ALARM NOW!")
                    os.popen("bensound-dubstep.mp3")
                    return
            time.sleep(60)
        except:
            return
    def just_die(self):
        self.keep_running = False



print("Enter your name: ")
user_input=input(":")

print("Hello, " + user_input)

alarm_HH = input("Enter the hour you want to wake up at: ")
alarm_MM = input("Enter the minute you want to wake up at: ")

print(("You want to wake up at: " + alarm_HH + ':' + alarm_MM).format(alarm_HH, alarm_MM))

alarm=("class Alarm")
class Alarm (Alarm(alarm_HH, alarm_MM)):
    alarm.start()

try:
    while True:
         text = str(user_input())
         if text == "stop":
            alarm.just_die()
            break

except:
    print("Yikes lets get out of here")
    alarm.just_die()

Tags: importselfinputtimedefhhclassmm
1条回答
网友
1楼 · 发布于 2024-03-29 06:42:31

我不完全确定你想用这个结束循环做什么,但是我相信它给你这个错误的原因是你试图引用用户的输入,就像它是一个函数一样。也许你只是想等用户输入什么?如果是的话。。。在

尝试改变这个:

text = str(user_input())

为此:

^{pr2}$

相关问题 更多 >