2017-08-09 1 views
-1

(サイコロのロールが1と6の間の整数の乱数を取得することによってシミュレートすることができます)サイコロの10本のロールでリストを入力します。私は一種の持っているコード割り当ては、私が表示しなければならないと言う

Display all 10 rolls 
Display all 10 rolls in backwards order 
Display the first and last rolls 
Display the sum of the rolls 
Display the min and the max value rolled. 

私は他の人に表示させる方法を理解できません。私はエラーコードを取得し続けます。ここで私はイライラになったので、私はこれまでのところ、私はそれのビットを削除したものです:

import random 

def roll(sides=6): 
     numRolled = random.randint(1,sides) 
     return num_rolled 
def rollDice(): 
     sides = 6 
     rolling = True 
     diceList=[] 
     while rolling: 
       numRolled = random.randint(1,sides) 
       diceList.append(numRolled) 
       print (numRolled, end="") 
       if len(diceList) == 10: 
        rolling = False 
     return diceList 

def main(): 
    print (rollDice()) 
    print (diceList) 
    print (rolldice.sort()) 
    print (rollDice[0],rolldice[9]) 
    print (rolldice.min,rolldice.max) 
    print (rolldice.sum) 
main() 
+2

あなたは決して関数の出力をどの変数にも割り当てません。関数内で特に定義されている 'diceList'は、直接外部にアクセスすることはできません。なぜなら、' rolldice'変数 –

答えて

0

あなたがサイコロの10巻でいっぱいにリストを生成するために、リスト内包表記を使用することができます。そしてあなたが望む結果を返します。

ten_roll_of_dice = [random.randint(1, 6) for _ in range(10)] 

print "Display all 10 rolls: ", ten_roll_of_dice 

print "Display all 10 rolls in backwards order: ", ten_roll_of_dice[::-1] 

print "Display the first and last rolls: %d, %d" % (ten_roll_of_dice[0], ten_roll_of_dice[-1]) 

print "Display the sum of the rolls: ", sum(ten_roll_of_dice) 

print "Display the min and the max value rolled: %d, %d" % (min(ten_roll_of_dice), max(ten_roll_of_dice)) 
+0

はあなたが私の絶対的な英雄です – ChelluhC

関連する問題