我的NIM游戏代码。计算机转弯代码出错

时间:2014-04-27 23:11:49

标签: python loops

#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 个答案:

答案 0 :(得分:2)

如您所见,random.randint()只接受 int s(整数)。

>>> 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,我们就不希望它使randint更高的数字(6)。

答案 1 :(得分:1)

错误在追溯中可见:

rand = random.randint(1,(0.5*marbles))

此函数调用的第二个值是float,而不是整数。 randint函数仅适用于整数参数。异常比平时更难理解,因为randintrandrange的包装器,其中引发了实际的异常。

要解决此问题,您需要强制您的参数为int的整数,或者以不同的方式进行计算(例如使用//分区运算符)。由于这看起来像是家庭作业,我会留下细节给你。

稍后您可能会遇到另一个问题,即您的playNovice函数不会返回所用的弹珠数量。你也可以修理那个!