2017-03-27 3 views
-2

私は基本的なゲームを2人の選手を1〜3のサイコロを選ぶことができます。ループを作成して終了できますか?

ロールをループして終了し、続行して、ロールするサイコロの数を選択して開始画面に移動する方法がわかりません。これのスコアを維持するために使用できるもの。これは私が持っているコードです。

コマンドがどのように私はrandomモジュールとrandint機能を使用してオプションのすべて

Dice = input("Please select number of dice you would like to use (1-3)") 
if Dice == "1": 
    print("You have selected 1 dice") 
import random 
Roll1 = random.randrange(1,6) 
print ("Player 1's Roll") 
print(Roll1) 
print ("Player 2's Roll") 
print (Roll1) 
#2 Dice Counter 
    if Dice == "2": 
    print("You have selected 2 dice") 
import random 
Roll2 = (random.randrange(2,12)) 
print ("Player 1's Roll") 
print(Roll2) 
print ("Player 2's Roll") 
print (Roll22) 

#3 Dice Counter 
if Dice == "3": 
    print("You have selected 3 dice") 
import random 
Roll3 = random.randrange(3,18) 
print ("Player 1's Roll") 
print(Roll3) 
print ("Player 2's Roll") 
print (Roll3) 
while invalid_input : 
      Dice() 

答えて

-1

を表示せずに1から3までのサイコロ選択をするならば、私もとのトラブルを抱えています。

from random import randint 

それを使用する:

randint(0,10) 

は上記ロールループに

使用して、whileループをループ0及び

10との間のランダムな整数を生成します。場合の については

condition = "play" 
while condition == "play": 
    #Your code here 

    if(input("Enter 'play' to play again or 'quit' to stop the program and reveal the scores: ") == "quit"): 
     #Break out of the loop 
     break 
    else: 
     condition = "play" 

私はrandintまたはrandrangeで数値の範囲を生成するために、いくつかの数学を使用します。例:

#Get input of dice to roll as integer 
toRoll = int(input("Dice to roll: ")) 
#'Roll' the dice that many times 
Roll = random.randrange(1*toRoll,6*toRoll) 

次に、変数Rollを使用します。 これは、すべてのif文(もちろんprintを除く)を使用するのと同じ結果になります。

+0

まず、:、今このメッセージを終了し、さよならするループwhile (inputValue != -1)が終了し、乱数を生成するためのrandintを使用し、チェック次のスニペットために印刷されますが発生しますユーザ入力-1まで、ループ内getDiceInput()を呼び出しておきますOPはすでにそれをやっています。次に、コードに構文エラーがあります。 – TigerhawkT3

+0

彼は 'randint'を使用しておらず、構文エラーはどこですか? –

+0

彼らは非常に似通った 'randrange'を使用しています。これは、実際の質問のいずれの部分にも対処しません。 – TigerhawkT3

0

ロジックを関数getDiceInput()に入れます。ユーザーが有効なオプションを入力した場合に戻ります。(1-3)の有効な入力が入力された場合は結果が出力されます。無効な入力メッセージが出力されます。

from random import randint 


    def getDiceInput(): 
     Roll1 = 0 
     Roll2 = 0 
     validInput = 1 
     Dice = input("Please select number of dice you would like to use (1-3) -1 to exit") 
     if Dice == "1": 
     print("You have selected 1 dice") 
     Roll1 = int(randint(1,6)) 
     Roll2 = int(randint(1,6)) 

     #2 Dice Counter 
     elif Dice == "2": 
     print("You have selected 2 dice") 
     Roll1 = int(randint(2,12)) 
     Roll2 = int(randint(2,12)) 

     #3 Dice Counter 
     elif Dice == "3": 
     print("You have selected 3 dice") 
     Roll1 = randint(3,18) 
     Roll2 = randint(3,18) 

     elif Dice == "-1": 
     validInput = -1 

     else : 
     print("invalid input reenter") 
     validInput = 0 


     if validInput == 1 : 
     print ("Player 1's Roll") 
     print(Roll1) 
     print ("Player 2's Roll") 
     print (Roll2) 

     return validInput 

    #Code execution will start here 
    inputValue = getDiceInput() 
    while (inputValue != -1): 
     inputValue = getDiceInput() 

    print("Goodbye") 
+0

ありがとうございます、これをさらに説明してください。どのような機能が使用されているか/実行中 –

+0

更新された回答を確認してください。 –

関連する問題