我在def函数中有如下代码,我希望执行结果展示是水平而不是垂直展示的,最好也不要带括号
values=random.randint(1,6), random.randint(1,6), random.randint(1,6), random.randint(1,6), random.randint(1,6)
print("\nYou rerolled some dice and the new values are:")
print("Die 1:", values[0])
print("Die 2:", values[1])
print("Die 3:", values[2])
print("Die 4:", values[3])
print("Die 5:", values[4])
return values
输出结果如下:
You rerolled some dice and the new values are:
Die 1: 2
Die 2: 1
Die 3: 2
Die 4: 5
Die 5: 6
我希望它这样展示:
You rolled some dice and the new values are: 2, 1, 2, 5, 6
import random
N = 5 # number of dice throws
values = [0] * N
for i in range(N):
values[i] = random.randint(1,6)
# Remove brackets
str_values = [str(i) for i in values] # convert to strings
new_values = ", ".join(str_values)
print("\nYou rerolled some dice and the new values are: {}".format(new_values))
输出结果如下:
You rerolled some dice and the new values are: 1, 1, 6, 1, 5
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。