2016-12-23 5 views
-1

私はこれを作って動作させていますが、ちょっと厄介なので、100%動作させたいと思っています。これは、コード変数を取得してからPythonで空白のない文字列を入力する

import random 
input_1 = str(input("Do you want to roll a dice? Enter y for yes anything else for no. ")) 
if input_1 == "y": 
    dicesides = int(input("How many sides do you want your dice to have? ")) 
    diceroll = random.randint(1, dicesides) 
    print("The dice rolled a",diceroll ".") 
    input_2 = str(input("Do you want to roll the dice again? Same keys. ")) 
    while input_2 == "y": 
     print(random.randint(1, dicesides)) 
     input_2 = str(input("Do you want to roll the dice again? Same keys. ")) 
    else: 
     print("Okay, goodbye now.") 

else: 
    print("Okay, goodbye now.") 

コードは、それは私に、それは「ロール」番号を伝える実行しますが、それは数の後にスペースを入れています。私はこの振る舞いを望まない。 2番目の+ダイスロールをどのように変更するかを知りました。

ありがとう!

+0

'プリントを(、diceroll「サイコロはロール」「」)'無効な構文です。私はあなたが 'print("サイコロは{}を巻いた。 ")書式(diceroll)' ' –

答えて

1

print機能のヘルプページをご覧ください。引数のリストが指定されている場合、引数はsepの値で区切られて表示されます。これはデフォルトでは1つのスペースです。したがって:

print("This",1,"is",2,"spaced",3,"out.") 
print("This",1,"is",2,"not.",sep="") 

は異なる動作をします。あなたはこのように、sep=""を使用して手動で文字列に独自の末尾または先頭のスペースを追加することをお勧めします:

print("You rolled a ", roll, ".", sep="") 
関連する問題