Найти - Пользователи
Полная версия: проблема с циклами
Начало » Python для новичков » проблема с циклами
1
ss100s
 import subprocess, threading
s = 'say hello world'
num = 3
while num > 0:
    if 'say' in s:
        p = subprocess.Popen('date', shell = True, stdout = subprocess.PIPE)
        output = p.communicate()[0]
        print output
    else:
        print ('no')
num=num-1
Отрабатывает бесконечно.
А нужно только 3 раза.
vic57
 >>> num = 3
>>> while num:
	print(num)
	num-=1
	
3
2
1
>>> 
marvellik
вычитание единицы начнется только после завершения цикла а цикл увы бесконечный так как num не меняется. все дело в отступе в 11 строке
 import subprocess, threading
s = 'say hello world'
num = 3
while num > 0:
    if 'say' in s:
        p = subprocess.Popen('date', shell = True, stdout = subprocess.PIPE)
        output = p.communicate()[0]
        print (output)
    else:
        print ('no')
    num  = num-1
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB