Python - run two commands at the same time

后端 未结 4 440
生来不讨喜
生来不讨喜 2021-01-07 08:05

I am new to Python and am having trouble with this piece of code:

while true:
   rand = random.choice(number)
   print(rand)             
   enter_word = inp         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-01-07 08:44

    I want to be able to input words in the console while, at the same time, have random numbers appear in the console.

    #!/usr/bin/env python
    import random
    
    def print_random(n=10):
        print(random.randrange(n)) # print random number in the range(0, n)
    
    stop = call_repeatedly(1, print_random) # print random number every second
    while True:
       word = raw_input("Write something: ") # ask for input until "quit" 
       if word == "quit":
          stop() # stop printing random numbers
          break # quit 
    

    where call_repeatedly() is define here.

    call_repeatedly() uses a separate thread to call print_random() function repeatedly.

提交回复
热议问题