Abhiram Reddy Abhiram Reddy - 8 days ago 5
Python Question

alarm with mathematical problems

I am using python to create an alarm clock which asks mathematical problems.But my problem is I want to continue asking questions if the user gives wrong answer.Please Help me with that.I am using tkinter to create alarm clock.But in the askyesno,yes and no only yes is executing.So please help me to improve the code

enter code here
from tkinter import *
from tkinter import ttk
import time
import os
import sys
from tkinter import messagebox

root = Tk()
root.title("Alarm clock")
def SubmitButton():
AlarmTime= enter1.get()
Message1()
CurrentTime = time.strftime("%H:%M")
print("the alarm time is: {}".format(AlarmTime))
while AlarmTime != CurrentTime:
CurrentTime = time.strftime("%H:%M")
time.sleep(1)
x = 0
y = 0
while AlarmTime == CurrentTime:
if x == 0:
x+=1
os.system("start alarm-music.mp3")
if y<1:
label2.config(text = "Alarm music playing.....")
messagebox.showinfo(title= 'Alarm Message', message= " {}".format(enter2.get()))

while True:
if y<1:
messagebox.askquestion("answer the question in yes or no","(9^5)-1+(4|3)=18",icon='question')
y+=1
if True:
break
else:

os.system("start alarm-music.mp3")
messagebox.askyesno("answer the question","9*71=647",icon='question')
if True:
sys.close()
else:
os.system("start alarm-music.mp3")

def Message1():
AlarmTimeLable= enter1.get()
label2.config(text="the Alarm time is Counting...")
messagebox.showinfo(title = 'Alarm clock', message = 'Alarm will Ring at {}'.format(AlarmTimeLable))
alarm1 = ttk.Frame(root)
alarm1.pack()
alarm1.config(height = 200, width = 200)

alarmtime1= ttk.Label(alarm1,text = "Enter the Alarm time :")
alarmtime1.pack()


enter1 = ttk.Entry(alarm1, width = 100)
enter1.pack()
enter1.insert(3,"Please enter 24-hr format here")

AlarmMessage= ttk.Label(alarm1, text="Alarm Message:")
AlarmMessage.pack()

enter2= ttk.Entry(alarm1, width=100)
enter2.pack()

button1= ttk.Button(alarm1, text= "submit", command=SubmitButton)
button1.pack()
label2= ttk.Label(alarm1)
label2.pack()



root.mainloop()

Answer

This code

if True:
        break

always breaks your cycle. To avoid this try to save result of dialog to any variable and then validate it value:

if y<1:
   answer = messagebox.askquestion("answer the question in yes or no","(9^5)-1+(4|3)=18",icon='question')
   y+=1
   if answer == 'yes':
       break
...
answer = messagebox.askyesno("answer the question","9*71=647",icon='question')
if answer  == True:
    sys.close()
else:
    os.system("start alarm-music.mp3")
...