user3382238
user3382238

Reputation: 141

My code for NIM game. Error in computer's turn code

#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')

An error occurs after the computer has taken its marbles. Alot of the time i find it hard to understand what the error code means, or at least how to fix it.

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()

Upvotes: 0

Views: 1678

Answers (2)

A.J. Uppal
A.J. Uppal

Reputation: 19264

As you can see, random.randint() only takes ints (integers).

>>> import random
>>> random.randint(1, 10)
6
>>> random.randint(1, 10)
2

However,if you use a float, it gives you the same error:

>>> 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()
>>> 

Here is your updated code

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')

This uses math.floor to get the bottom integer, so that if marbles is 11, we don't want it taking the randint of a number higher (6).

Upvotes: 2

Blckknght
Blckknght

Reputation: 104722

The error is visible in the traceback:

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

The second value of this function call will be a float, rather than an integer. The randint function only works with integer arguments. The exception is a little harder to understand than usual though because randint is a wrapper around randrange where the actual exception is raised.

To fix the issue you need to either force your argument to be an integer with int, or perhaps do the calculation in a different way (such as using the // floor division operator). Since this looks like a homework assignment, I'll leave the details to you.

You'll probably run into an additional issue though, later, which is that your playNovice function doesn't return the number of marbles taken. You may want to fix that one too!

Upvotes: 1

Related Questions