- File MyRNG.py contain the class MyRNG which run perfectly on its own after setting up seed and high variable
- class MyRNG will be imported to the file guess.py which should be run on command line like this: guess.py -s 1 -m 0 -M 13. However, this error message comes up:
C:\Users\Davy\Desktop>cd python
C:\Users\Davy\Desktop\Python>guess.py -s 1 -m 1 -M 13
Traceback (most recent call last):
File "C:\Users\Davy\Desktop\Python\guess.py", line 53, in <module>
main( )
File "C:\Users\Davy\Desktop\Python\guess.py", line 47, in main
print r.next()
File "C:\Users\Davy\Desktop\Python\MyRNG.py", line 20, in next
self.ranNum = (self.multiplier * self.se) % self.high #Error: Not all argume
nts converted during string formatting
TypeError: not all arguments converted during string formatting
#Name: Davy Uong
#File name: MyRNG.py
#A simple random generator class
class MyRNG:
"""A simple random generator class"""
#constructor
def __init__(self):
self.high = 0
self.low = 0
self.multiplier = 6
self.ranNum = 0
#Seed the generator with seed x
def seed(self,x):
self.se = x #Set the se variable
#Return the next random number
def next(self):
self.ranNum = (self.multiplier * self.se) % self.high #Error: Not all arguments converted during string formatting
self.se = self.se + 1 #Increment se
return self.ranNum
guess.py file
#File name: guess.py
import sys
import getopt
# Importing class file MyRNG.py
from MyRNG import *
def usage( status, msg = "" ):
if msg:
print msg
print "guess.py [-h] [-v] [-s seed] -m 0 -M 400"
sys.exit( status )
def main( ):
r = MyRNG() #Create an object r = MyRNG class
verbose = False
try:
opts, args = getopt.getopt( sys.argv[1:], "hvs:m:M:" )
except getopt.GetoptError, msg:
usage( 1, msg )
for o, a in opts:
# o has the option, and a has the argument.
if o == "-h":
# Print out the usage message and exit.
usage( 0 )
elif o == "-v":
# Set verbose mode to be true for debugging messages
verbose = True
elif o == "-s":
# set the seed here (change the code below to suit your needs)
seed = a
r.seed(seed) # Set the initial seed
elif o == "-m":
# set the minimum here (change the code below to suit your needs)
minimum = a
r.low = minimum # Set the low
elif o == "-M":
# set the maximum here (change the code below to suit your needs)
maximum = a
r.high = maximum # Set the high
# If you have imported your RNG, then the code below will print out
# 10 random numbers
for x in range(10):
print r.next()
if verbose:
print "This message only appears if verbose mode is turned on."
if __name__ == "__main__":
main( )
I search around to find a fix but I am too newbie to understand. If you know please show me.
Thanks in advance
Davy

New Topic/Question
Reply




MultiQuote




|