Python 如何将while循环生成的字符串存储到变量中
我想将stdout.write函数打印的所有“o”存储到一个变量中,该变量可以随时访问 我曾尝试使用len函数在循环达到一定数量的字符串时中断循环,但没有成功Python 如何将while循环生成的字符串存储到变量中,python,python-3.x,sys,Python,Python 3.x,Sys,我想将stdout.write函数打印的所有“o”存储到一个变量中,该变量可以随时访问 我曾尝试使用len函数在循环达到一定数量的字符串时中断循环,但没有成功 import time import sys while True: sys.stdout.write("o") sys.stdout.flush() time.sleep(0.05) 继续向字符串追加值。检查长度并在需要时断开 import time import sys data = "" while
import time
import sys
while True:
sys.stdout.write("o")
sys.stdout.flush()
time.sleep(0.05)
继续向字符串追加值。检查长度并在需要时断开
import time
import sys
data = ""
while True:
temp = "o"
data += temp
sys.stdout.write(temp)
sys.stdout.flush()
time.sleep(0.05)
if(len(data)==10):
break;
您可以在单独的变量中跟踪O的数量:
number_of_os = 0
while True:
sys.stdout.write("o")
sys.stdout.flush()
number_of_os += 1
if number_of_os >= 100:
break
time.sleep(0.05)
非常简单,您可以将它们添加到字符串中,一次添加一个:
record = ""
while True:
sys.stdout.write("o")
sys.stdout.flush()
record += 'o'
time.sleep(0.05)
一种稍微快一点的方法是计算写入的数量,然后生成所需的字符串:
count = 0
while True:
sys.stdout.write("o")
sys.stdout.flush()
count += 1
time.sleep(0.05)
# Insert your exit condition
record = 'o' * count