Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
[Python] PyQuiz - (Maths, Tabulate and SQLite)
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Moogly" data-source="post: 385943" data-attributes="member: 16998"><p>Aside from using tkinter to make it more approachable, anytime you repeat the same code like a SQL query, you could parametize it and use format as necessary:</p><p>[code]</p><p>query_str = 'INSERT INTO Class{0}(FirstName, SecondName, Score) VALUES(?,?,?)'.format(classnum)</p><p>[/code]</p><p>Then instead of:</p><p>[code]</p><p>with con:</p><p>if classnum == '1':</p><p> cur = con.cursor()</p><p> cur.execute('''INSERT INTO Class1(FirstName, SecondName, Score) VALUES(?,?,?)''', (firstname,secondname,correct))</p><p> con.commit()</p><p>elif classnum == '2':</p><p> cur = con.cursor()</p><p> cur.execute('''INSERT INTO Class2(FirstName, SecondName, Score) VALUES(?,?,?)''', (firstname,secondname,correct))</p><p> con.commit()</p><p>elif classnum == '3':</p><p> cur = con.cursor()</p><p> cur.execute('''INSERT INTO Class3(FirstName, SecondName, Score) VALUES(?,?,?)''', (firstname,secondname,correct))</p><p> con.commit()</p><p>[/code]</p><p>You could do:</p><p>[code]</p><p># with con line goes before this block:</p><p>cur = con.cursor()</p><p>query_str = 'INSERT INTO Class{0}(FirstName, SecondName, Score) VALUES(?,?,?)'.format(classnum)</p><p>cur.execute(query_str, (firstname,secondname,correct))</p><p>con.commit()</p><p>[/code]</p><p></p><p>You would get rid of 9 lines of code and accomplish the same. If you ever find yourself repeating yourself in code, chances are you can find a quicker way in less lines of code to accomplish the same. It may take a while to grasp sometimes, but the more willing you are to fix up your code the better you will be and closer you will become to being a Pythonista. Anyway: great job, maybe for your next challenge try adding Tkinter as an interface, it's not as hard as it sounds. I was able to use tkinter with little to no Python experience before.</p></blockquote><p></p>
[QUOTE="Moogly, post: 385943, member: 16998"] Aside from using tkinter to make it more approachable, anytime you repeat the same code like a SQL query, you could parametize it and use format as necessary: [code] query_str = 'INSERT INTO Class{0}(FirstName, SecondName, Score) VALUES(?,?,?)'.format(classnum) [/code] Then instead of: [code] with con: if classnum == '1': cur = con.cursor() cur.execute('''INSERT INTO Class1(FirstName, SecondName, Score) VALUES(?,?,?)''', (firstname,secondname,correct)) con.commit() elif classnum == '2': cur = con.cursor() cur.execute('''INSERT INTO Class2(FirstName, SecondName, Score) VALUES(?,?,?)''', (firstname,secondname,correct)) con.commit() elif classnum == '3': cur = con.cursor() cur.execute('''INSERT INTO Class3(FirstName, SecondName, Score) VALUES(?,?,?)''', (firstname,secondname,correct)) con.commit() [/code] You could do: [code] # with con line goes before this block: cur = con.cursor() query_str = 'INSERT INTO Class{0}(FirstName, SecondName, Score) VALUES(?,?,?)'.format(classnum) cur.execute(query_str, (firstname,secondname,correct)) con.commit() [/code] You would get rid of 9 lines of code and accomplish the same. If you ever find yourself repeating yourself in code, chances are you can find a quicker way in less lines of code to accomplish the same. It may take a while to grasp sometimes, but the more willing you are to fix up your code the better you will be and closer you will become to being a Pythonista. Anyway: great job, maybe for your next challenge try adding Tkinter as an interface, it's not as hard as it sounds. I was able to use tkinter with little to no Python experience before. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
[Python] PyQuiz - (Maths, Tabulate and SQLite)
Top