掷骰子直到你得到 6
Roll the dice until you get 6
我应该编写代码来掷骰子并自动循环直到结果为 6 然后停止。我的代码只滚动一次并无限重复。
import random
min=1
max=6
r=(random.randint(min, max))
while r < 6:
print(r)
continue
if r == 6:
print(r)
break```
您需要在每次迭代时生成随机数,如下所示:
import random
min_=1
max_=6
while True:
r = random.randint(min_, max_)
print(r)
if r == 6:
print(r)
break
您必须再次掷骰子 (也称为循环内):
import random
mi, ma = 1, 6 # do not shadow built-ins `min` and `max`
while True:
r = random.randint(mi, ma)
print(r)
if r == 6:
break
或者使用赋值表达式(使用海象运算符):
while (r := random.randint(mi, ma)) != 6:
print(r)
# print(r) # if you need to see the final 6
我应该编写代码来掷骰子并自动循环直到结果为 6 然后停止。我的代码只滚动一次并无限重复。
import random
min=1
max=6
r=(random.randint(min, max))
while r < 6:
print(r)
continue
if r == 6:
print(r)
break```
您需要在每次迭代时生成随机数,如下所示:
import random
min_=1
max_=6
while True:
r = random.randint(min_, max_)
print(r)
if r == 6:
print(r)
break
您必须再次掷骰子 (也称为循环内):
import random
mi, ma = 1, 6 # do not shadow built-ins `min` and `max`
while True:
r = random.randint(mi, ma)
print(r)
if r == 6:
break
或者使用赋值表达式(使用海象运算符):
while (r := random.randint(mi, ma)) != 6:
print(r)
# print(r) # if you need to see the final 6