2013-04-28 2 views
0

번호 옆에 기호가 있으면 가능한 난수가 생성 된 횟수를 표시하고 싶습니다. 그러나이 같은 숫자 아래에 새 줄에 기호를두고있다 :파이썬에서 난수 빈도 표시

Dice rolled: 
You rolled 2 and 4 

Roll again [y|n]? n 

Number Frequency: 
1 
2 
x 
3 
4 
x 
5 
6 

어떻게 그 숫자 옆에 같은 줄에 기호를 표시 할 수 있을까?

import random 

diceCount = [0,0,0,0,0,0,0] 
roll='y' 

while roll=='y': 
    die1=random.randint(1,6) 
    die2=random.randint(1,6) 
    diceCount[die1] = diceCount[die1] + 1 
    diceCount[die2] = diceCount[die2] + 1 

    print('Dice rolled:') 
    print('You rolled',die1,'and',die2) 

    if roll=='y': 
     roll=input('\nRoll again [y|n]? ') 
     while roll!='y' and roll!='n': 
      roll=input("Please enter either 'y' or 'n': ") 

if roll=='n': 

     print('\nFace Freqeuncy:') 
     index=1 
     while (index<len(diceCount)): 
      print(index) 
      for number in range (diceCount[index]): 
       print(' x') 
      index+=1 

답변

2

파이썬 3에서는 선택 매개 변수 end을 사용하여 개행을 제거 할 수 있습니다.

print(index, end="") 

나는, 당신도 같은 줄에 모든 x을 할 것입니다 가정하는 경우, print(' x', end="");와 동일한 작업을 수행하고 루프 후 줄 바꿈을 추가하고있다.

+1

이것은 OP 도움이되지 않습니다. 카운트되지 않은 모든 값은 동일한 행에있게됩니다. –

0

올바르게 출력하려면 루프를 수정해야합니다. 루프가 조기에 종료되지 않으면 for 루프의 else 절이 방문됩니다.

while (index<len(diceCount)): 
      print(index,end="") #let the next print appear on same line 
      for number in range (diceCount[index]): 
       print(' x',end="") #print the correct number of x's on the same line 
      print() #we now need to print a newline 
      index+=1 

예 :

Dice rolled: 
You rolled 1 and 5 
Roll again [y|n]? n 
Face Freqeuncy: 
1 x 
2 
3 
4 
5 x 
6 

Dice rolled: 
You rolled 6 and 6 
Roll again [y|n]? y 
Dice rolled: 
You rolled 3 and 4 
Roll again [y|n]? n 
Face Freqeuncy: 
1 
2 
3 x 
4 x 
5 
6 x x 
요아킴은 무엇을 게시 외에도
1

, 당신은 또한 collections.Counter 사용할 수 있습니다

from collections import Counter 

rolls = [] 
roll = 'y' 

while roll=='y': 
    die1=random.randint(1,6) 
    die2=random.randint(1,6) 
    rolls.append(die1) 
    rolls.append(die2) 

    print('Dice rolled:') 
    print('You rolled',die1,'and',die2) 

    if roll=='y': 
     roll=input('\nRoll again [y|n]? ') 
     while roll!='y' and roll!='n': 
      roll=input("Please enter either 'y' or 'n': ") 

counted_rolls = Counter(rolls) 

for i range(1,7): 
    print("{} {}".format(i,'x'*counted_rolls.get(i,0))) 
+0

+1. 나는'collections'에 대해 말하는 모든 대답을 좋아합니다. – StoryTeller

+0

당신이 이것을 작성한 방법은 처음에는 'roll ='y ''가 필요하지 않습니까? – DSM

+0

예, 원래 코드에 이미 있습니다. 카운터의 비트를 추가했습니다. 업데이트 됨. –

1

시험해보기 :

롤링 주사위를위한 클래스를 만들었습니다. 여기에서 각 롤러와 측면의 주사위 양을 사용자 정의하고 롤을 추적 할 수 있습니다.

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 
+0

OP는 파이썬 3을 사용합니다. – DSM

+0

@DSM은 프린트를 파이썬 3으로 고정했습니다. –