使用“random.randint”时出错 "too many positional arguments"
Error using `random.randint` "too many positional arguments"
我在调用 random.randint() 时遇到问题。我的程序中收到以下错误消息。我正在使用 python 3.8,我不确定为什么会这样。以下是错误信息。
Traceback (most recent call last):
File "C:\Users\W\Desktop\All.py files\droll.py", line 13, in <module>
roll=random.randint(1,
TypeError: randint() takes 3 positional arguments but 7 were given
这是我的代码
import random
def rule():
print ("Roll the die!")
print ("If you get a 1, you lose,")
print ("And if you get a 6, you win.")
print ("Anything inbetween does not count.")
rule()
#Main game loop
while True:
q = input ("Are you ready to roll? (Y/N)").lower().strip()
if q == "y":
roll=random.randint(1,
2,
3,
4,
5,
6)
if (roll == 1):
print ("You got a 1! You lost!")
if (roll == 6):
print ("You got a 6! You won!")
else:
print ("You got a middle roll!")
if q == "n":
print ("That's unfortunate.")
如果有人能帮助解决这个问题,我们将不胜感激。我不确定是否有一种新的方法来编写随机代码,这已经困扰了我一段时间,因为即使是最简单的代码也行不通。我试图通过添加更多 random.randint 并使用 if 和语句来解决此问题,但是有时会导致 2 个答案或空白 space。如果有人有答案,我将再次感谢您的帮助。
谢谢
random.randint
接受两个参数——下限和上限。
尝试
roll = random.randint(1,6)
而不是
roll = random.randint(1,
2,
3,
4,
5,
6)
A description of random.randint
in the Python documentation.
是的,有多种方法可以生成随机(和伪随机)数,常用的函数包括:
choice() :- 用于从容器中生成随机数。
roll = random.choice([1, 2, 3, 4, 5, 6])
randrange(beg, end, step) :- 用于生成指定范围内的随机数(不包括end)
roll = random.randrange(1, 7, 1)
randint() :- 另一种方式...
roll = random.randint(1,6)
我在调用 random.randint() 时遇到问题。我的程序中收到以下错误消息。我正在使用 python 3.8,我不确定为什么会这样。以下是错误信息。
Traceback (most recent call last):
File "C:\Users\W\Desktop\All.py files\droll.py", line 13, in <module>
roll=random.randint(1,
TypeError: randint() takes 3 positional arguments but 7 were given
这是我的代码
import random
def rule():
print ("Roll the die!")
print ("If you get a 1, you lose,")
print ("And if you get a 6, you win.")
print ("Anything inbetween does not count.")
rule()
#Main game loop
while True:
q = input ("Are you ready to roll? (Y/N)").lower().strip()
if q == "y":
roll=random.randint(1,
2,
3,
4,
5,
6)
if (roll == 1):
print ("You got a 1! You lost!")
if (roll == 6):
print ("You got a 6! You won!")
else:
print ("You got a middle roll!")
if q == "n":
print ("That's unfortunate.")
如果有人能帮助解决这个问题,我们将不胜感激。我不确定是否有一种新的方法来编写随机代码,这已经困扰了我一段时间,因为即使是最简单的代码也行不通。我试图通过添加更多 random.randint 并使用 if 和语句来解决此问题,但是有时会导致 2 个答案或空白 space。如果有人有答案,我将再次感谢您的帮助。 谢谢
random.randint
接受两个参数——下限和上限。
尝试
roll = random.randint(1,6)
而不是
roll = random.randint(1,
2,
3,
4,
5,
6)
A description of random.randint
in the Python documentation.
是的,有多种方法可以生成随机(和伪随机)数,常用的函数包括:
choice() :- 用于从容器中生成随机数。
roll = random.choice([1, 2, 3, 4, 5, 6])
randrange(beg, end, step) :- 用于生成指定范围内的随机数(不包括end)
roll = random.randrange(1, 7, 1)
randint() :- 另一种方式...
roll = random.randint(1,6)