Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: cannot switch from manual field specification to automatic field numbering

The class:

class Book(object):
    def __init__(self, title, author):
        self.title = title
        self.author = author

    def get_entry(self):
        return "{0} by {1} on {}".format(self.title, self.author, self.press)

Create an instance of my book from it:

In [72]: mybook = Book('HTML','Lee')
In [75]: mybook.title
Out[75]: 'HTML'
In [76]: mybook.author
Out[76]: 'Lee'

Please notice that I didn't initialize attribute 'self.press',while use it in the get_entry method.Go ahead to type in data.

mybook.press = 'Murach'
mybook.price = 'download'

Till now, I can specify all the data input with vars

In [77]: vars(mybook)
Out[77]: {'author': 'Lee', 'title': 'HTML',...}

I hardtype lot of data about mybook in the console.When try to call get_entry method, errors report.

mybook.get_entry()
ValueError: cannot switch from manual field specification to automatic field numbering.

All this going in interactive mode on console.I cherish the data inputed, further to pickle mybook object in file. However, it is flawed. How can rescue it in the interactive mode. or I have to restart all over again.

like image 257
AbstProcDo Avatar asked Oct 16 '17 10:10

AbstProcDo


3 Answers

return "{0} by {1} on {}".format(self.title, self.author, self.press)

that doesn't work. If you specify positions, you have to do it through the end:

return "{0} by {1} on {2}".format(self.title, self.author, self.press)

In your case, best is to leave python treat that automatically:

return "{} by {} on {}".format(self.title, self.author, self.press)
like image 104
Jean-François Fabre Avatar answered Nov 18 '22 19:11

Jean-François Fabre


Well if can give a proper output in a table format if instead of using format go for f"" ;

for e.g

<!DOCTYPE html>
<html>
<head>
    <title><strong>Unable to handle Value Error</strong></title>
</head>
<body>
<p><ol>for name, branch,year in college:</ol>
         <ol> print(f"{name:{10}} {branch:{20}} {year:{12}} )</ol>
    
    <ol>name       branch               year    </ol>
    <ol>ankit      cse                         2</ol>
    <ol>vijay      ece                         4</ol>
<ol>    raj        IT                          1</ol>

</body>
</html>
like image 41
vineet27 Avatar answered Nov 18 '22 21:11

vineet27


print ("{0:.1f} and the other no {0:.2f}".format(a,b)) 

python cannot do both manual and automatic precision handling (field numbering) in a single execution of code. You can either go for specifying the field numbering for each variable or let python do it automatically for all.

like image 2
Taalib Zama Avatar answered Nov 18 '22 21:11

Taalib Zama