Python 在嵌套for循环(“*”之前打印while循环(1,10)

Python 在嵌套for循环(“*”之前打印while循环(1,10),python,for-loop,while-loop,Python,For Loop,While Loop,我试图用PYTHON编写代码,记录用户在“中间”玩骰子游戏的结果,然后在游戏结束时打印掷骰子的统计数据,所以我想打印的基本内容如下 Game Summary ============ You played 3 Games: |--> Games won: 0 |--> Games lost: 3 Dice Roll Stats: Face Frequency 1 2 * 3 4 ** 5 * 6 * 7 8 * 9 * 10 ** Thanks for playing! 其中一个

我试图用PYTHON编写代码,记录用户在“中间”玩骰子游戏的结果,然后在游戏结束时打印掷骰子的统计数据,所以我想打印的基本内容如下

Game Summary
============
You played 3 Games:
|--> Games won: 0
|--> Games lost: 3

Dice Roll Stats:
Face Frequency
1
2 *
3
4 **
5 *
6 *
7
8 *
9 *
10 **
Thanks for playing!
其中一个“*”是永远打印的,模具面是滚动的,但我一直以这样的方式结束

Game Summary
============

You played a total of 3 games:
|--> Games won: 1
|--> Games lost: 2

Dice Roll Stats.

Face  Frequency
 1
 2
 ** 3
 4
 5
 * 6
 * 7
 **** 8
 * 9
 10
 Thanks for playing!
所以我要做的是垂直排列'*',与索引值(1,10)相同,而不是'*'总是放在索引值的前面。 这是我的代码:)

die1=random.randint(1,10)
dieCount[die1]=dieCount[die1]+1
die2=random.randint(1,10)
diecont[die2]=diecont[die2]+1
die3=random.randint(1,10)
死亡人数[die3]=死亡人数[die3]+1
死亡人数=[0,0,0,0,0,0,0,0,0,0,0]
索引=1
而指数
试试这个:

while index < len(dieCount):

    print(index,end ="")
    for n in range(dieCount[index]):   
        print('*', end='')
    print()
    index = index + 1
索引 打印(索引,end=“”) 对于范围内的n(dieCount[索引]): 打印('*',结束='') 打印() 索引=索引+1 试试这个:

while index < len(dieCount):

    print(index,end ="")
    for n in range(dieCount[index]):   
        print('*', end='')
    print()
    index = index + 1
索引 打印(索引,end=“”) 对于范围内的n(dieCount[索引]): 打印('*',结束='') 打印() 索引=索引+1
您可以这样打印,一次打印整行:

for i, val in enumerate(dieCount[1:], 1):
    print('{} {}'.format(i, '*' * val))

您可以这样打印,一次打印整行:

for i, val in enumerate(dieCount[1:], 1):
    print('{} {}'.format(i, '*' * val))
检查

复制于此:

我已经为掷骰子创建了一个类,在这个类中,您可以自定义每个骰子辊和侧面的骰子数量,以及跟踪骰子辊

import random
from collections import defaultdict

class roller():

    def __init__(self, number_of_dice=2, dice_sides=6):

        self.dice = defaultdict(dict)
        for die in range(number_of_dice):
            self.dice[die]['sides'] = dice_sides
            self.dice[die]['count'] = dict((k,0) for k in range(1, dice_sides+1))

    def roll(self, times=1):
        print ("Rolling the Dice %d time(s):" % times)
        total = 0
        for time in range(times):
            roll_total = 0
            print ("Roll %d" % (time+1))
            for die, stats in self.dice.items():
                result = random.randint(1, stats['sides'])
                roll_total += result
                stats['count'][result] += 1
                print (" Dice %s, sides: %s, result: %s" % (die, stats['sides'], result))
            print ("Roll %d total: %s" % (time+1, roll_total))
            total += roll_total
        print ("Total result: %s" % total)


    def stats(self):
        print ("Roll Statistics:")
        for die, stats in self.dice.items():
            print (" Dice %s, sides: %s" % (die, stats['sides'])) 
            for value, count in stats['count'].items():
                print ("  %s: %s times" % (value, count))
使用它:

>>> a = roller()
>>> a.roll(4)
Rolling the Dice 4 time(s):
Roll 1
 Dice 0, sides: 6, result: 6
 Dice 1, sides: 6, result: 3
Roll 1 total: 9
Roll 2
 Dice 0, sides: 6, result: 3
 Dice 1, sides: 6, result: 3
Roll 2 total: 6
Roll 3
 Dice 0, sides: 6, result: 1
 Dice 1, sides: 6, result: 6
Roll 3 total: 7
Roll 4
 Dice 0, sides: 6, result: 5
 Dice 1, sides: 6, result: 4
Roll 4 total: 9
Total result: 31
>>> a.stats()
Roll Statistics:
 Dice 0, sides: 6
  1: 1 times
  2: 0 times
  3: 1 times
  4: 0 times
  5: 1 times
  6: 1 times
 Dice 1, sides: 6
  1: 0 times
  2: 0 times
  3: 2 times
  4: 1 times
  5: 0 times
  6: 1 times
检查

复制于此:

我已经为掷骰子创建了一个类,在这个类中,您可以自定义每个骰子辊和侧面的骰子数量,以及跟踪骰子辊

import random
from collections import defaultdict

class roller():

    def __init__(self, number_of_dice=2, dice_sides=6):

        self.dice = defaultdict(dict)
        for die in range(number_of_dice):
            self.dice[die]['sides'] = dice_sides
            self.dice[die]['count'] = dict((k,0) for k in range(1, dice_sides+1))

    def roll(self, times=1):
        print ("Rolling the Dice %d time(s):" % times)
        total = 0
        for time in range(times):
            roll_total = 0
            print ("Roll %d" % (time+1))
            for die, stats in self.dice.items():
                result = random.randint(1, stats['sides'])
                roll_total += result
                stats['count'][result] += 1
                print (" Dice %s, sides: %s, result: %s" % (die, stats['sides'], result))
            print ("Roll %d total: %s" % (time+1, roll_total))
            total += roll_total
        print ("Total result: %s" % total)


    def stats(self):
        print ("Roll Statistics:")
        for die, stats in self.dice.items():
            print (" Dice %s, sides: %s" % (die, stats['sides'])) 
            for value, count in stats['count'].items():
                print ("  %s: %s times" % (value, count))
使用它:

>>> a = roller()
>>> a.roll(4)
Rolling the Dice 4 time(s):
Roll 1
 Dice 0, sides: 6, result: 6
 Dice 1, sides: 6, result: 3
Roll 1 total: 9
Roll 2
 Dice 0, sides: 6, result: 3
 Dice 1, sides: 6, result: 3
Roll 2 total: 6
Roll 3
 Dice 0, sides: 6, result: 1
 Dice 1, sides: 6, result: 6
Roll 3 total: 7
Roll 4
 Dice 0, sides: 6, result: 5
 Dice 1, sides: 6, result: 4
Roll 4 total: 9
Total result: 31
>>> a.stats()
Roll Statistics:
 Dice 0, sides: 6
  1: 1 times
  2: 0 times
  3: 1 times
  4: 0 times
  5: 1 times
  6: 1 times
 Dice 1, sides: 6
  1: 0 times
  2: 0 times
  3: 2 times
  4: 1 times
  5: 0 times
  6: 1 times

第一次打印(索引)似乎是在之后自动添加换行符。试着像打印*的方式那样进行:

    print(index, end='')

第一次打印(索引)似乎是在之后自动添加换行符。试着像打印*的方式那样进行:

    print(index, end='')

我不想把它们放在同一行上,我想把1-10的数字从上到下列出来,1-2-3-4-5-6-7-8-9-10,然后在这些数字的右边有星星,表示模具表面向右滚动了多少次,但是如果你有打印(索引),它会自动添加一个新的行,然后放上你的星星。在打印完星星后,你得到的打印(“”,end=“”)不会在后面创建新行。所以你是这样的。。。print(index)\n print(“”,end=“”)print(index)\n print(“”,end=“”)print(index)\n您可以在以下位置尝试:print(index,end=“”)forloop[print(“”,end=“”)]print()我不希望它们在同一行,我正试图从上到下列出1-10之间的数字,1 2 3 4 5 6 7 8 9 10,然后在这些数字的右侧有星星,表示模具面向右滚动了多少次,但如果有打印(索引),它会自动添加一条新行,然后放置星星。在打印完星星后,你得到的打印(“”,end=“”)不会在后面创建新行。所以你是这样的。。。打印(索引)\n打印(“”,end=“”)打印(索引)\n打印(“”,end=“”)打印(索引)\n您可以尝试以下操作:打印(index,end=“”)forloop[print(“”,end=“”)]print()