我正在尝试创建一个倒数计时器,但它不起作用

时间:2015-03-25 18:14:07

标签: python timer

我正在尝试制作一个Python编程的计时器;这里有什么不对吗?

#CTimer

import os;
import time

#vl

repeater = 2

sleeptime = 0.00

#code

while repeater==2:
print("-CTimer-")
print("\n")
print("Set time")
print("Use preset")
print("Exit")
print("\n")
var1 = input(">>>")

if var1=="set time":

    sleeptime = input("Set time:")

    start = input("Start")

    time.sleep(sleeptime)

    os.system("C:/Python34/Scripts/alsctimer64an.bat")

if var1=="use preset":

    print("1min")
    print("5min")
    print("10min")
    print("20min")
    print("30min")
    print("45min")
    print("1hour")
    print("\n")
    usepreset = input("Use preset:")

    if usepreset=="1min":

        start = input("Start")

        time.sleep(60)

        os.system("C:/Python34/Scripts/alsctimer64an.bat");

    if usepreset=="5min":

        start = input("Start")

        time.sleep(300)

        os.system("C:/Python34/Scripts/alsctimer64an.bat");

    if usepreset=="10min":

        start = input("Start")

        time.sleep(600)

        os.system("C:/Python34/Scripts/alsctimer64an.bat");

    if usepreset=="20min":

        start = input("Start")

        time.sleep(1200)

        os.system("C:/Python34/Scripts/alsctimer64an.bat");

    if usepreset=="30min":

        start = input("Start")

        time.sleep(1800)

        os.system("C:/Python34/Scripts/alsctimer64an.bat");

    if usepreset=="45min":

        start = input("Start")

        time.sleep(2700)

        os.system("C:/Python34/Scripts/alsctimer64an.bat");

    if usepreset=="1hour":

        start = input("Start")

        time.sleep(3600)

        os.system("C:/Python34/Scripts/alsctimer64an.bat");

if var1=="exit":

    print("Exiting...")

    time.sleep(5)

    exit()

2 个答案:

答案 0 :(得分:1)

输入类型

需要做类型转换用户输入值。因为sleeptime变量的数据类型是字符串,而time.sleep()方法需要float或int。类型转换也是如此。

演示:

>>> import time
>>> sleeptime = raw_input("Set time:")
Set time:1.2
>>> sleeptime
'1.2'
>>> type(sleeptime)
<type 'str'>
>>> time.sleep(sleeptime)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required
>>> time.sleep(float(sleeptime))
>>> 

类型转换期间的异常处理

用户可能输入错误的值,因此在“类型转换”期间添加异常处理代码。

演示:

>>> try:
...     sleeptime = float(raw_input("Set time:"))
... except ValueError:
...     print "Value Error, Enter only float value"
...     sleeptime = 0.0
... 
Set time:we
Value Error, Enter only float value
>>> try:
...     sleeptime = float(raw_input("Set time:"))
... except ValueError:
...     print "Value Error, Enter only float value"
...     sleeptime = 0.0
... 
Set time:10.1
>>> print sleeptime, type(sleeptime)
10.1 <type 'float'>
>>> 

使用 if-elif-else 循环

演示:

if usepreset=="1min":
    pass
    #code
elif usepreset=="5min":
    pass
    #code
elif usepreset=="10min":
    pass
    #code
else:
    print ("Enter Wrong Input.")

无需额外变量repeater = 2,使用True1或任何真值(不得使用0False,{{1} })

''

在代码中,在下一行

进行类型转换
While True:
   # add your code.

注意:

对Python 3.x使用input()

对Python 2.x使用raw_input()

答案 1 :(得分:1)

您只需添加float()即可使sleeptimer函数可读取time.sleep()变量:

#CTimer

import os;
import time

#vl

repeater = 2

sleeptime = 0.00

#code

while repeater==2:
    print("-CTimer-")
    print("\n")
    print("Set time")
    print("Use preset")
    print("Exit")
    print("\n")
    var1 = input(">>>")

    if var1=="set time":

        sleeptime = float(input("Set time:"))  #<-- add float()

        start = input("Start")

        time.sleep(sleeptime)

        os.system("C:/Python34/Scripts/alsctimer64an.bat")

    if var1=="use preset":

        print("1min")
        print("5min")
        print("10min")
        print("20min")
        print("30min")
        print("45min")
        print("1hour")
        print("\n")
        usepreset = input("Use preset:")

        if usepreset=="1min":

            start = input("Start")

            time.sleep(60)

            os.system("C:/Python34/Scripts/alsctimer64an.bat");

        if usepreset=="5min":

            start = input("Start")

            time.sleep(300)

            os.system("C:/Python34/Scripts/alsctimer64an.bat");

        if usepreset=="10min":

            start = input("Start")

            time.sleep(600)

            os.system("C:/Python34/Scripts/alsctimer64an.bat");

        if usepreset=="20min":

            start = input("Start")

            time.sleep(1200)

            os.system("C:/Python34/Scripts/alsctimer64an.bat");

        if usepreset=="30min":

            start = input("Start")

            time.sleep(1800)

            os.system("C:/Python34/Scripts/alsctimer64an.bat");

        if usepreset=="45min":

            start = input("Start")

            time.sleep(2700)

            os.system("C:/Python34/Scripts/alsctimer64an.bat");

        if usepreset=="1hour":

            start = input("Start")

            time.sleep(3600)

            os.system("C:/Python34/Scripts/alsctimer64an.bat");

    if var1=="exit":

        print("Exiting...")

        time.sleep(5)

        exit()