import random
while True:
num_side = int(input("Which type of dice do You wish to roll? Select from: [4, 6, 8, 10, 12, 20, 100]: "))
num_dice = int(input("How many dice do You wish to roll?: "))
rolls = []
for die in range(num_dice):
dice_roll = random.randint(1, num_side)
rolls.append(dice_roll)
print ("You rolled: {}".format(rolls))
當 num_dice 輸入 == 大于 1 時,輸出將列印在多行上,例如,如果 num_dice == 4 輸出看起來像
You rolled: [x]
You rolled: [x,x]
You rolled: [x,x,x]
You rolled: [x,x,x,x]
我怎樣才能解決這個問題以在一行上列印卷?
uj5u.com熱心網友回復:
將print()陳述句移到一個縮進上,結果只會在所有骰子都擲完后才會出現。
import random
while True:
num_side = int(input("Which type of dice do You wish to roll? Select from: [4, 6, 8, 10, 12, 20, 100]: "))
num_dice = int(input("How many dice do You wish to roll?: "))
rolls = []
for die in range(num_dice):
dice_roll = random.randint(1, num_side)
rolls.append(dice_roll)
print("You rolled: {}".format(rolls))
uj5u.com熱心網友回復:
替換列印陳述句如下
print ("You rolled: {}".format(rolls),end=" ")
uj5u.com熱心網友回復:
就像
step your print() function out of your for loop
下一個
import random
while True:
num_side = int(input("Which type of dice do You wish to roll? Select from: [4, 6, 8, 10, 12, 20, 100]: "))
num_dice = int(input("How many dice do You wish to roll?: "))
rolls = []
for die in range(num_dice):
dice_roll = random.randint(1, num_side)
rolls.append(dice_roll)
print ("You rolled: {}".format(rolls))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/417411.html
標籤:
