Is there a better way to loop code?

James Y.

I am teaching myself python and I am trying to a code to open the cd drive at a certain time. For reasons of testing I have been using time.clock() because it only uses numbers, but I want to use time.ctime() so that the program will work at a specific time. This is the code that I have so far.

import time
import ctypes
if(time.clock()==10):
    ctypes.windll.winmm.mciSendStringW("set cdaudio door open",
            None, 0, None)
for x in range(10**100):
    print(x)
    if(20>time.clock()>10):
        ctypes.windll.winmm.mciSendStringW("set cdaudio door open",
            None, 0, None)
        quit()

I am using the print(x) function to monitor the code, and I set the range high enough that it won't stop before reaching the number of seconds the door should to open.

Peter Gibson

Instead of for x in range(10**100): you could use

while True:
    # do stuff forever

This loop will continue until you explicitly break out (or return for a function, raise an exception or quit the program etc).

time.ctime() returns a string, which is not good for comparing to a given time. Instead, I would suggest using the datetime module

go_time = datetime.datetime(2014, 9, 3, 12, 30)
while True:
    now = datetime.datetime.now()
    if now >= go_time:
        # open the draw etc...
        quit()

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Is there a better way to do this code? Perhaps using filter or reduce?

分類Dev

I need a better way to code this Random Letter generator

分類Dev

how to change for loop code to pythonic way

分類Dev

Is there a better way of comparing dates

分類Dev

A better way to detect the cursor?

分類Dev

Better way to return boolean

分類Dev

A better way to introspect a capture

分類Dev

Is there a better way to do this? BASH

分類Dev

better way to do this in MATLAB?

分類Dev

Better way to write this jQuery code consisting of many links and open/closing divs

分類Dev

Is there a better way to define a global variable?

分類Dev

Decorator with Arguments: Would this be a better way?

分類Dev

Is this possible using LEAD or is there a better way?

分類Dev

Is there a way of making discord embeds better?

分類Dev

Is there a better way to do Insertion Sort?

分類Dev

Is there a better way to do this than echo?

分類Dev

Better way to override a function definition

分類Dev

Is there a better way to divide this string into substrings?

分類Dev

a better version of the following code (python)

分類Dev

PHP: Is there a better function to loop through multidimensional array

分類Dev

Is there a better way to re-use plots Matplotlib?

分類Dev

Better way of capturing multiple same tags?

分類Dev

Better way to execute nested for loops to generate a dict?

分類Dev

Better way of error handling in Kafka Consumer

分類Dev

rxJava better way to wait on a specific sequence of values

分類Dev

Which way of setting fields value is better and why?

分類Dev

Which way of setting fields value is better and why?

分類Dev

AngularJS: Is there a better way to achieve this than the one specified?

分類Dev

Better way to write jquery add/remove class

Related 関連記事

  1. 1

    Is there a better way to do this code? Perhaps using filter or reduce?

  2. 2

    I need a better way to code this Random Letter generator

  3. 3

    how to change for loop code to pythonic way

  4. 4

    Is there a better way of comparing dates

  5. 5

    A better way to detect the cursor?

  6. 6

    Better way to return boolean

  7. 7

    A better way to introspect a capture

  8. 8

    Is there a better way to do this? BASH

  9. 9

    better way to do this in MATLAB?

  10. 10

    Better way to write this jQuery code consisting of many links and open/closing divs

  11. 11

    Is there a better way to define a global variable?

  12. 12

    Decorator with Arguments: Would this be a better way?

  13. 13

    Is this possible using LEAD or is there a better way?

  14. 14

    Is there a way of making discord embeds better?

  15. 15

    Is there a better way to do Insertion Sort?

  16. 16

    Is there a better way to do this than echo?

  17. 17

    Better way to override a function definition

  18. 18

    Is there a better way to divide this string into substrings?

  19. 19

    a better version of the following code (python)

  20. 20

    PHP: Is there a better function to loop through multidimensional array

  21. 21

    Is there a better way to re-use plots Matplotlib?

  22. 22

    Better way of capturing multiple same tags?

  23. 23

    Better way to execute nested for loops to generate a dict?

  24. 24

    Better way of error handling in Kafka Consumer

  25. 25

    rxJava better way to wait on a specific sequence of values

  26. 26

    Which way of setting fields value is better and why?

  27. 27

    Which way of setting fields value is better and why?

  28. 28

    AngularJS: Is there a better way to achieve this than the one specified?

  29. 29

    Better way to write jquery add/remove class

ホットタグ

アーカイブ