开发者社区> 问答> 正文

在Python中怎样让执行结果水平展示而不是垂直展示?

我在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

展开
收起
安忆333 2019-11-29 11:53:10 617 0
1 条回答
写回答
取消 提交回答
  • 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
    
    2019-11-29 11:58:15
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载