我如何连接一个随机的randint?

时间:2015-07-15 17:05:34

标签: python

好吧所以我对python非常非常新,我正在制作一个骰子游戏我试图使用随机randint函数使用字符串和随机数连接,但它只显示一个长的无意义错误消息,例如: 请原谅我的缩进我试图制作一个程序,模拟在agian上滚动一个公平的骰子直到达到50

3
you chose the number<bound method Random.randint of <random.Random object at 0x02804790>>woop
5
you chose the number<bound method Random.randint of <random.Random object at 0x02804790>>woop
2
you chose the number<bound method Random.randint of <random.Random object at 0x02804790>>woop
2
you chose the number<bound method Random.randint of <random.Random object at 0x02804790>>woop
4
you chose the number<bound method Random.randint of <random.Random object at 0x02804790>>woop

这是我的代码可以任何人帮助我,以便一旦添加的所有骰子数加起来代码停止50:

    import random
     score = 0
     poss_ans = 'yes'
     poss_ans1= 'no'
     roll_1 = 0
     count = True
     for i in range(50):
       one = 0
        two = 0
       three = 0
       four = 0
        five = 0
        six = 0
        number_6=raw_input("do u wanna play")
       if number_6 == poss_ans:
     print("okay")
      elif number_6 == poss_ans1:
     print("weirdo")
    score = 0
   while count < 50:
     print(random.randint (1, 6))
      print ("you chose the number" + str(random.randint) + "woop")
        count += 1

3 个答案:

答案 0 :(得分:1)

您应该将random.randint返回的数字连接起来:"Hello"+str(random.randint(1,6))

您的问题是因为您尝试将函数转换为字符串。那是可能的。我在Python 2.7中得到以下内容:

>>>str(random.randint)
 '<bound method Random.randint of <random.Random object at 0x1653720c>>'

答案 1 :(得分:0)

您可以使用format方法

print ("you chose the number {} woop".format(random.randint(1,6))

答案 2 :(得分:0)

如果你想在所有骰子数加起来为50时停止循环,你可以使用它:

    dice_list=[]

    while count < 50:
         dice_number = random.randint(1,6)
         print(dice_number)
         print ("you chose the number " + str(dice_number) + " woop")
         dice_list.append(dice_number)
         count = sum(dice_list)