Python Forum
user inputs in constructing a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
user inputs in constructing a dictionary
#1
I am making a dictionary, and I called for user input for the key-value combinations, and this was the result:

latin_vocab = {}

latin_vocab[input("Latin word: ")] = input("English meaning: ")

print(latin_vocab)
Quote:English meaning: what
Latin word: quid
{'quid': 'what'}

Process finished with exit code 0

I wasn't expecting it to ask me for the value before it asked me for the key. How do I reverse that (without swapping the content)?
Reply
#2
You'd have to do it in two steps:

latin = input('Latin word: ')
latin_vocab[latin] = input('English meaning: ')
Python (and most computer languages) evaluate what is on the right of the assignment first, and then try to figure out what they are assigning to on the left.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
How would I go about giving the user the ability to add an unlimited number of key-value entries? Presumably something with a while loop.

Never mind. I figured it out. I didn't realize I could use the same variable over and over again.

latin_vocab = {}
stop_word = ""

while stop_word.lower() != "stop":
    new_key = input("\nLatin word: ")
    latin_vocab[new_key] = input("English meaning: ")
    print(" ")
    print("Current list: " + str(latin_vocab))
    stop_word = input("\nStop? ")
Reply
#4
Here's a little design tweak:

while True:
    new_key = input("\nLatin word (or 'stop' to stop): ")
    if new_key == 'stop':
        break
    latin_vocab[new_key] = input("English meaning: ")
    print(" ")
    print("Current list: " + str(latin_vocab))
Now the user doesn't have to keep hitting return at the stop question to keep going.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to let the user add 'None' to an specific array in a dictionary? noahverner1995 4 1,735 Dec-26-2021, 10:03 AM
Last Post: noahverner1995
  How to write a response if a user inputs a string horuscope42 3 2,186 Apr-29-2020, 03:39 PM
Last Post: deanhystad
  User input & Dictionary tfernandes 5 3,593 Apr-03-2020, 07:12 PM
Last Post: tfernandes
  Perminantly saving user inputs + being able to retrieve it ThatOneGuyNoOneKnowsCodes 1 1,878 Oct-23-2019, 11:28 PM
Last Post: DT2000
  Trying to prompt user for 2 inputs on one line pythonprogrammer 2 2,459 Sep-15-2019, 04:41 PM
Last Post: snippsat
  unit testing a method that asks two user inputs() in console gebel 0 2,121 Apr-03-2019, 07:59 PM
Last Post: gebel
  User Input to Choose from Dictionary anelliaf 9 25,673 Mar-27-2018, 02:22 PM
Last Post: anelliaf
  question regarding user Inputs cibb 10 7,151 Apr-04-2017, 03:34 AM
Last Post: alicarlos13
  code that takes inputs for user name amounts etc and then sends custom message shaumyabrata 5 5,248 Feb-12-2017, 11:37 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020