好的,所以我是非常非常新的python,我正在制作一个骰子游戏,我试图使用一个字符串和一个随机数连接使用random randint函数,但它只是显示一个长没有意义的错误消息,例如:请原谅我的缩进,我正在尝试制作一个程序,模拟再次滚动一个均匀面骰子,直到达到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
您应该像这样连接random.randint
返回的数字:"Hello"+str(random.randint(1,6))
。
你的问题是因为你试图将函数转换为字符串。这是可能的。我在Python 2.7中得到了以下内容:
>>>str(random.randint)
'<bound method Random.randint of <random.Random object at 0x1653720c>>'
如果你想停止while循环,一旦所有骰子的数字加起来是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)
可以使用format
方法
print ("you chose the number {} woop".format(random.randint(1,6))
您也可以使用:
print ("you chose the number" + " %d "%random.randint(1,6) + "woop")
random.randint(a,b)接受参数a和b,它们是生成的随机数的范围,包括a和b。