2 Replies - 4590 Views - Last Post: 01 September 2011 - 10:16 AM Rate Topic: -----

#1 GreenOrc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 28-February 10

TypeError: not all arguments converted during string formatting.

Posted 01 September 2011 - 01:47 AM

Hello, this is the first time I deal with Python, the syntax is so much different than C++ which I am too used to. After couple hours I manage to get the random generator program like this:
- 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

Is This A Good Question/Topic? 0
  • +

Replies To: TypeError: not all arguments converted during string formatting.

#2 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: TypeError: not all arguments converted during string formatting.

Posted 01 September 2011 - 02:37 AM

When you're using getopt, you're using a to set the seed (on line 33). a is a string, as are all the arguments when using getopt. You can fix that by simply putting int() around a to convert it to an integer, but it won't protect against users entering characters and what not.

You should look into argparse, or optparse, depending on what version of Python you're using. Try for argparse. It's much easier than getopt, generates usage statements for you, and protects against problems like the one you're having now.
Was This Post Helpful? 2
  • +
  • -

#3 GreenOrc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 28-February 10

Re: TypeError: not all arguments converted during string formatting.

Posted 01 September 2011 - 10:16 AM

View PostHiram, on 01 September 2011 - 02:37 AM, said:

When you're using getopt, you're using a to set the seed (on line 33). a is a string, as are all the arguments when using getopt. You can fix that by simply putting int() around a to convert it to an integer, but it won't protect against users entering characters and what not.

You should look into argparse, or optparse, depending on what version of Python you're using. Try for argparse. It's much easier than getopt, generates usage statements for you, and protects against problems like the one you're having now.


Thank you

This post has been edited by GreenOrc: 01 September 2011 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1