2016-12-23 21 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.") 

當代碼運行時,它告訴我它「滾動」的數字,但它在數字後面留出空格。我不想要這種行爲。 當我發現我將如何改變第二個+骰子到它。

想通了,謝謝!

+0

'打印(「骰子滾了」,diceroll「」)'是無效的語法。我認爲你想'印刷(「骰子滾動{}。」。格式(diceroll))'' –

回答

1

查看print函數的幫助頁面。如果給出了一個參數列表,它會打印它們,參數爲sep,默認情況下爲單個空格。所以:

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

將表現不同。你會想用sep=""並添加自己的尾隨或前導空格手動你的字符串,像這樣:

print("You rolled a ", roll, ".", sep="") 
相關問題