将一行随机数加在一起,并在行的末尾打印总数

时间:2017-03-22 03:54:29

标签: python random

随机导入 进口时间

RollAgain =“是”

而RollAgain ==“是”或RollAgain ==“y”:

DiceThrows = int(input('How many Dice would you like to throw: '))
DiceType = int(input ('How many sides are on your dice: '))
throw = 0

print 'Rolling the Dice...'
for throw in range (DiceThrows):
    time.sleep(1)
    print(random.randint(1,DiceType)), # Comma prints number on same line
                                       How do you add them on the same line?
RollAgain = raw_input("Would you like to roll again? ")

1 个答案:

答案 0 :(得分:0)

您可以使用sys.stdout.write代替print来执行此操作。

import random 
import time
import sys
RollAgain = "yes"

while   RollAgain == "yes" or RollAgain == "y":

  DiceThrows = int(input('How many Dice would you like to throw: '))
  DiceType = int(input ('How many sides are on your dice: '))
  throw = 0

  print('Rolling the Dice...')
  sum = 0
  for throw in range (DiceThrows):
      time.sleep(1)
      no = random.randint(1,DiceType)
      sum += no
      sys.stdout.write(str(no) + " ")
  print("= %d" %sum)
  RollAgain = raw_input("Would you like to roll again? ")