-1
  1. Allows the user to submit a pairing
  2. Allows the user to delete a pairing
  3. Allows the user to submit final pairings

How can I edit the program so that the user (after completing 1, 2 or 3) i asked the same question again? (Question = user_selection)

clue_list = {'#':'A', '%':'N', '*':'M'}

user_selection = input('What would you like to do? 1.Submit a letter or symbol pairing,     2.Delete a letter/symbol pairing, 3. Submit Final Answers ')
while user_selection != '3':
if user_selection == '1':
    userkey = input('Please enter a symbol to add: ')
    uservalue = input('Please enter a letter to add: ')
    if userkey in clue_list:
        print('This symbol has already been matched. Please try again.')
    else:
        clue_list[userkey] = uservalue
    print(clue_list)
if user_selection == '2':
    user_delete_input = input('What letter/symbol would you like to delete? (Please enter symbol to delete the pairing ')
    if user_delete_input in clue_list:
        del clue_list[user_delete_input]
        print('That letter/symbol has been deleted.')
    else:
        print('Error: That letter/symbol has not been found in file.')                

user_submit1 = input('Would you like to submit more pairings? Yes/No ')
if user_submit1 == 'Yes':
    function_result2 = submit(clue_list)
else:
    if user_submit1 == 'No':
       print('...')
  • Given that you already know about `while` loops, it's hard to see what the problem is. – jonrsharpe Jul 21 '14 at 16:04
  • I do, but only a little bit. That is why I came here. – Calvin_Medcalf Jul 21 '14 at 16:07
  • SO isn't actually here to hold beginners' hands; please see [the Help Center](http://stackoverflow.com/help/asking) for what is considered on-topic. In the meantime, sections 3 and 4 of [the official tutorial](https://docs.python.org/2/tutorial/) cover `while` and `for` loops and control flow in general. Note that there are other questions that duplicate this one, e.g. http://stackoverflow.com/q/12608654/3001761 – jonrsharpe Jul 21 '14 at 16:09
  • @jonrsharpe While the question isn't the best phrasing, nor is it particularly novel, it is certainly no more hands-holding than some of the other beginner questions out there (which IMO are fine for this site once phrased properly). One should be careful about rejecting the questions from new (and young!) users just because they lack the knowledge to phrase a question properly. – Hooked Jul 21 '14 at 19:28
  • @Calvin_Medcalf Note that in pasting your code there are some indentation errors. Make sure these are properly checked before you click the submit button! – Hooked Jul 21 '14 at 19:39

1 Answers1

0

The crux of your question is getting the selection process to repeat. You can do so quite easily if you learn to abstract away parts of your code with functions. For example, a possible answer to your question looks like:

while True:
    user_selection = input(msg_ask).strip()
    if   user_selection == '1': add_symbol(clue_list)
    elif user_selection == '2': remove_symbol(clue_list)
    elif user_selection == '3': break

Where we've defined the functions add_symbol and remove_symbol like so:

clue_list = {'#':'A', '%':'N', '*':'M'}

msg_ask = '''
What would you like to do?
1. Submit a letter or symbol pairing,
2. Delete a letter/symbol pairing,
3. Submit Final Answers
'''

def add_symbol(clue_list):
    userkey = input('Please enter a symbol to add: ')
    uservalue = input('Please enter a letter to add: ')
    if userkey in clue_list:
        print('This symbol has already been matched.')
    else:
        clue_list[userkey] = uservalue
        print(clue_list)

def remove_symbol(clue_list):
    msg = 'What letter/symbol would you like to delete? (Please enter symbol to delete the pairing) '
    user_delete_input = input(msg)
    if user_delete_input in clue_list:
        del clue_list[user_delete_input]
        print('That letter/symbol has been deleted.')
    else:
        print('Error: That letter/symbol has not been found in file.')               

You'll notice how the first code block is much easier to read.

Hooked
  • 84,485
  • 43
  • 192
  • 261