[英]I have written one code in python 2 now i want to execute this in python3, i am getting error
[英]I've written code where I loop a random - the code works but now I want to add strings between every new number
我已经编写了循环random.randint
的代码。 该代码工作正常,但现在我想在每个新数字之间添加字符串,而不是同时打印所有数字并将其变成棋盘游戏。 我是编码新手,希望有人可以帮助我,避免拼写错误或羞辱我。
这是我的代码
import random
for i in range(20):
dice=random.randint(1,6)
print(dice)
我尝试使用范围(1)制作单独的for循环,但是当我运行代码时它们再次打印数字,数字仍然一起打印,我不知道如何停止它。 我什至尝试使用返回 function 但随后出现语法错误
import random
for i in range(1):
dice = random.randint(1,6)
print(dice)
for i in range(1):
dice2 = random.randint(1,6)
print(dice2)
for i in range(1):
dice3 = random.randint(1,6)
print(dice3)
for i in range(1):
dice4 = random.randint(1,6)
print(dice4)
for i in range(1):
dice5 = random.randint(1,6)
print(dice5)
for i in range(1):
dice6 = random.randint(1,6)
print(dice6)
for i in range(1):
dice7 = random.randint(1,6)
print(dice7)
我有一个想法做一些相关的切片,但我不确定
对不起,我没有完全理解你的问题。 但也许您没有将随机 int 转换为字符串? 你必须这样做才能打印一些东西。 像这样:
import random
for i in range(20):
dice = random.randint(1, 6)
# now convert it to a string to print it with str()
print(str(dice))
我希望这可以帮到你:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.