我的NIM游戏代码。计算机回合代码错误
#Question 1
def playNovice(marbles):
import random
rand = random.randint(1,(0.5*marbles))
print('Computer takes: ', rand)
#Question 2
def userPlay(marbles):
userAmount = marbles
while userAmount > (marbles * 0.5):
userAmount = int(input("Input Number of Marbles you want to take from pile: "))
return userAmount
#Question 4
import random
marbles = random.randint(10,100)
print("Size of Marble Pile: ", marbles)
OneOrZero = random.randint(0,1)
starter = int(input("Enter a Number between 0 and 1 to decide who starts: "))
while marbles > 1:
if starter == OneOrZero:
while marbles > 1:
print("Your Turn!")
rand = userPlay(marbles)
marbles = (marbles - rand)
print(marbles, 'Marbles Remaining')
print("Computers Turn!")
rand = playNovice(marbles)
marbles = (marbles - rand)
print(marbles, 'Marbles Remaining')
elif starter != OneOrZero:
while marbles > 1:
print("Computers Turn!")
rand = playNovice(marbles)
marbles = (marbles - rand)
print(marbles, 'Marbles Remaining')
print("Your Turn!")
rand = userPlay(marbles)
marbles = (marbles - rand)
print(marbles, 'Marbles Remaining')
当电脑出现问题时,常常会显示一个错误代码。很多时候,我发现自己很难理解这个错误代码的意思,或者至少不知道该怎么解决它。
Traceback (most recent call last):
File "/Users/ruairimangan-cliff/Documents/Work:Personal/Engineering Foundation/Semester 2/Foundations of Computer Programming/NIM/Coursework Assignment (NIM) 2.py", line 44, in <module>
playNovice(marbles)
File "/Users/ruairimangan-cliff/Documents/Work:Personal/Engineering Foundation/Semester 2/Foundations of Computer Programming/NIM/Coursework Assignment (NIM) 2.py", line 23, in playNovice
rand = random.randint(1,(0.5*marbles))
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/random.py", line 213, in randint
return self.randrange(a, b+1)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/random.py", line 186, in randrange
raise ValueError("non-integer stop for randrange()")
ValueError: non-integer stop for randrange()
2 个回答
1
错误信息可以在追踪记录中看到:
rand = random.randint(1,(0.5*marbles))
这个函数调用的第二个值是一个浮点数,而不是整数。randint
函数只接受整数作为参数。不过,这个异常的理解可能比平常要复杂一些,因为 randint
是一个包装函数,它实际上是调用了 randrange
,真正的错误是在这里发生的。
要解决这个问题,你需要用 int
强制把你的参数变成整数,或者可以考虑用其他方法来计算(比如使用 //
这个整除运算符)。看起来这像是一个作业,我就不详细说明了。
不过,你可能还会遇到另一个问题,就是你的 playNovice
函数没有返回拿走的弹珠数量。你可能也想把这个问题解决一下!
2
正如你所看到的,random.randint()
只接受 整数(int)。
>>> import random
>>> random.randint(1, 10)
6
>>> random.randint(1, 10)
2
但是,如果你使用 浮点数(float),它会给你同样的错误:
>>> random.randint(1, 10.5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 240, in randint
return self.randrange(a, b+1)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 196, in randrange
raise ValueError, "non-integer stop for randrange()"
ValueError: non-integer stop for randrange()
>>>
这是你更新后的代码
import math
#Question 1
def playNovice(marbles):
import random
rand = random.randint(1,math.floor(0.5*marbles))
print('Computer takes: ', rand)
#Question 2
def userPlay(marbles):
userAmount = marbles
while userAmount > (marbles * 0.5):
userAmount = int(input("Input Number of Marbles you want to take from pile: "))
return userAmount
#Question 4
import random
marbles = random.randint(10,100)
print("Size of Marble Pile: ", marbles)
OneOrZero = random.randint(0,1)
starter = int(input("Enter a Number between 0 and 1 to decide who starts: "))
while marbles > 1:
if starter == OneOrZero:
while marbles > 1:
print("Your Turn!")
rand = userPlay(marbles)
marbles = (marbles - rand)
print(marbles, 'Marbles Remaining')
print("Computers Turn!")
rand = playNovice(marbles)
marbles = (marbles - rand)
print(marbles, 'Marbles Remaining')
elif starter != OneOrZero:
while marbles > 1:
print("Computers Turn!")
rand = playNovice(marbles)
marbles = (marbles - rand)
print(marbles, 'Marbles Remaining')
print("Your Turn!")
rand = userPlay(marbles)
marbles = (marbles - rand)
print(marbles, 'Marbles Remaining')
这里使用了 math.floor 来获取最小的整数,这样如果 marbles
是 11,我们就不希望它取一个更高的随机整数(比如 6)。