How to get a random value from dictionary?

One way would be: import random d = {‘VENEZUELA’:’CARACAS’, ‘CANADA’:’OTTAWA’} random.choice(list(d.values())) EDIT: The question was changed a couple years after the original post, and now asks for a pair, rather than a single item. The final line should now be: country, capital = random.choice(list(d.items()))

How do I check if the user is pressing a key?

In java you don’t check if a key is pressed, instead you listen to KeyEvents. The right way to achieve your goal is to register a KeyEventDispatcher, and implement it to maintain the state of the desired key: import java.awt.KeyEventDispatcher; import java.awt.KeyboardFocusManager; import java.awt.event.KeyEvent; public class IsKeyPressed { private static volatile boolean wPressed = false; … Read more

Get the new record primary key ID from MySQL insert query?

You need to use the LAST_INSERT_ID() function: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id Eg: INSERT INTO table_name (col1, col2,…) VALUES (‘val1’, ‘val2’…); SELECT LAST_INSERT_ID(); This will get you back the PRIMARY KEY value of the last row that you inserted: The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned … Read more