在 python 中练习循环时遇到问题
Having problems with practicing loop in python
import time
import random
import math
start = 1
stop = 10
for i in range(start, stop):
time.sleep(1)
print("\ni = {}".format(i), end = "")
count = 3 + random.randint(1,5)
print(", count = {}".format(count))
nums = []
while len(nums) < count:
for j in range(count):
time.sleep(0.3)
try:
num = random.randint(-10,100)
log = round(math.log(num),1)
except:
time.sleep(2)
print("\nagain")
break
nums.append(log)
print("\rnums = {}".format(nums), end = "")
我想要做的是为每次迭代 i
提供一个随机整数 count
,然后使用 while
循环将 num
的日志附加到 nums
直到 nums
的长度等于 count
。这里num
是-10到100之间的随机整数,如果num
给定为0或以下,我想从for i in range(start, stop):
的最开始重新开始循环,也就是time.sleep(1)
,这样我就可以指定 new count
和 make new nums
.
于是我执行了上面的代码,它又从while len(nums) < count:
开始循环了。
例如,我期望的是这样的
i = 1, count = 7
nums = [4.2, 4.4, 2.9, 4.4]
again
i = 1, count = 5
nums = [3.5, 3.6, 4.2]
again
i = 1, count = 6
nums = [2.0, 3.2, 3.5, 3.6, 4.2, 4.5]
i = 2, count = 5
nums = [4.5, 4.1, 4.0, 3.9, 3.8]
i = 3, count = 6
nums = [4.3, 3.8]
again
i = 3, count = 5
nums = [3.0, 3.4, 4.6, 3.6, 3.1]
等等
而我得到的是
i = 1, count = 7
nums = [4.2, 4.4, 2.9, 4.4, 3.2, 2.6]
again
nums = [4.2, 4.4, 2.9, 4.4, 3.2, 2.6, 4.0, 1.4, 3.5, 3.6, 3.5, 4.4, 2.9]
i = 2, count = 4
again
nums = [4.5, 4.1, 4.0, 3.9]
i = 3, count = 6
nums = [4.3, 3.8]
again
nums = [4.3, 3.8, 3.6, 2.3, 3.4, 4.6, 3.6, 3.1]
i = 4, count = 5
nums = [1.4, 3.0, 4.0, 4.4, 4.0]
i = 5, count = 7
nums = [4.4, 2.9, 3.5, 4.2, 3.6, 4.4, 4.6]
等等
把它全部放在一个函数中,每当 num
小于或等于 0
时再次调用它
import time
import random
import math
start = 1
stop = 10
def whatever():
for i in range(start, stop):
time.sleep(1)
print("\ni = {}".format(i), end = "")
count = 3 + random.randint(1,5)
print(", count = {}".format(count))
nums = []
while len(nums) < count:
for j in range(count):
time.sleep(0.3)
num = random.randint(-10,100)
if num <= 0:
time.sleep(2)
print("\nagain")
whatever()
else:
log = round(math.log(num),1)
nums.append(log)
print("\rnums = {}".format(nums), end = "")
whatever()
如果 num
小于或等于 0
,循环将从这里再次开始
if num <= 0:
time.sleep(2)
print("\nagain")
whatever()
输出:
i = 1, count = 8
nums = [4.2, 2.9, 3.8, 2.1, 4.0, 4.1, 3.8, 4.4]
i = 2, count = 5
nums = [3.4, 3.2, 3.6, 4.5]
again
i = 1, count = 8
nums = [3.5, 4.2, 4.3, 3.9, 3.5, 4.3, 1.1, 3.2]
i = 2, count = 7
again
i = 1, count = 8
nums = [2.4, 3.0, 4.5, 4.1, 4.2, 4.5, 3.6]
import time
import random
import math
start = 1
stop = 10
for i in range(start, stop):
time.sleep(1)
print("\ni = {}".format(i), end = "")
count = 3 + random.randint(1,5)
print(", count = {}".format(count))
nums = []
while len(nums) < count:
for j in range(count):
time.sleep(0.3)
try:
num = random.randint(-10,100)
log = round(math.log(num),1)
except:
time.sleep(2)
print("\nagain")
break
nums.append(log)
print("\rnums = {}".format(nums), end = "")
我想要做的是为每次迭代 i
提供一个随机整数 count
,然后使用 while
循环将 num
的日志附加到 nums
直到 nums
的长度等于 count
。这里num
是-10到100之间的随机整数,如果num
给定为0或以下,我想从for i in range(start, stop):
的最开始重新开始循环,也就是time.sleep(1)
,这样我就可以指定 new count
和 make new nums
.
于是我执行了上面的代码,它又从while len(nums) < count:
开始循环了。
例如,我期望的是这样的
i = 1, count = 7
nums = [4.2, 4.4, 2.9, 4.4]
again
i = 1, count = 5
nums = [3.5, 3.6, 4.2]
again
i = 1, count = 6
nums = [2.0, 3.2, 3.5, 3.6, 4.2, 4.5]
i = 2, count = 5
nums = [4.5, 4.1, 4.0, 3.9, 3.8]
i = 3, count = 6
nums = [4.3, 3.8]
again
i = 3, count = 5
nums = [3.0, 3.4, 4.6, 3.6, 3.1]
等等
而我得到的是
i = 1, count = 7
nums = [4.2, 4.4, 2.9, 4.4, 3.2, 2.6]
again
nums = [4.2, 4.4, 2.9, 4.4, 3.2, 2.6, 4.0, 1.4, 3.5, 3.6, 3.5, 4.4, 2.9]
i = 2, count = 4
again
nums = [4.5, 4.1, 4.0, 3.9]
i = 3, count = 6
nums = [4.3, 3.8]
again
nums = [4.3, 3.8, 3.6, 2.3, 3.4, 4.6, 3.6, 3.1]
i = 4, count = 5
nums = [1.4, 3.0, 4.0, 4.4, 4.0]
i = 5, count = 7
nums = [4.4, 2.9, 3.5, 4.2, 3.6, 4.4, 4.6]
等等
把它全部放在一个函数中,每当 num
小于或等于 0
import time
import random
import math
start = 1
stop = 10
def whatever():
for i in range(start, stop):
time.sleep(1)
print("\ni = {}".format(i), end = "")
count = 3 + random.randint(1,5)
print(", count = {}".format(count))
nums = []
while len(nums) < count:
for j in range(count):
time.sleep(0.3)
num = random.randint(-10,100)
if num <= 0:
time.sleep(2)
print("\nagain")
whatever()
else:
log = round(math.log(num),1)
nums.append(log)
print("\rnums = {}".format(nums), end = "")
whatever()
如果 num
小于或等于 0
if num <= 0:
time.sleep(2)
print("\nagain")
whatever()
输出:
i = 1, count = 8
nums = [4.2, 2.9, 3.8, 2.1, 4.0, 4.1, 3.8, 4.4]
i = 2, count = 5
nums = [3.4, 3.2, 3.6, 4.5]
again
i = 1, count = 8
nums = [3.5, 4.2, 4.3, 3.9, 3.5, 4.3, 1.1, 3.2]
i = 2, count = 7
again
i = 1, count = 8
nums = [2.4, 3.0, 4.5, 4.1, 4.2, 4.5, 3.6]