Ben Franklin
Ben Franklin

Reputation: 27

allowing the user to input two parameters

i want to allow the user to be able to type {misc} of {nation} and specific information will be displayed based on the input. i have tired several different ways but seem to never find a way to make it work. any help is appreciated thanks!

(Sorry about title didnt really know what to title it)

def part1():
    txt = "{} of {}"
    info = input('''What do you want to know?: ''')
    if info == "{} of {}":
        print(txt.format(misc.get(info), nations.get(info)))
    else:
         print("i dont know what your talking about")
nations = {
    'fiji': {
        'location': 'Oceana',
        'capital_city': 'Suva'},
    'france': {
        'location': 'Europe',
        'capital_city': 'Paris'},
}

  misc = {'population': {'fiji': '847,706',
  'france': '60,495,540'},
'location': {'fiji': 'Oceana',
  'france': 'Europe'},
}
part1()

Upvotes: 1

Views: 102

Answers (4)

DannyMoshe
DannyMoshe

Reputation: 6255

Not sure exactly what you're trying to do, but there are a few issues with your code, try something like this:

def part1():
    txt = "{} of {}"
    info = input('''What do you want to know?: ''')
    if ' of ' in info:
        params = info.split(' of ')
    else:
         print("i dont know what your talking about")
         return

    print(params[0])
    print(misc.get(params[0]).get(params[1]))

nations = {
    'fiji': {
        'location': 'Oceana',
        'capital_city': 'Suva'},
    'france': {
        'location': 'Europe',
        'capital_city': 'Paris'},
}

misc = {'population': {'fiji': '847,706',
  'france': '60,495,540'},
'location': {'fiji': 'Oceana',
  'france': 'Europe'},
}
part1()

Currently with this code there isnt really a way to know which object you're targeting (nations or misc). To remedy this I would ask the user an initial question to determine which type of info they want.

Additional improvements could be checking the data you're getting from the dictionaries. In the case you get nothing from the dictionaries (.get returns None) you could notify the user the info you have available).

Upvotes: 3

Anonymous
Anonymous

Reputation: 33

You could also try a split method input().split(x, y)

x, y = input("What do you want to know?").split()

This might work

Or you could try a list

Upvotes: 0

Anonymous
Anonymous

Reputation: 33

A good way to do this would be if else/elif statements that way the program can check for multiple inputs rather then trying to check for 2 different inputs at once.

Viktor Basharkevich has pretty much the right idea on what to do

Upvotes: 0

Viktor Basharkevich
Viktor Basharkevich

Reputation: 17

A quick and dirty way to do this if you are not using much data, is to just put the info directly into the if statements and call the function. For instance:

def part1():
info = input("What do you want to know?: ")
if info == "location of fiji":
    print("Oceana:")
elif info == "capital_city of fiji":
    print("Suva")
elif info == "location of France":
    print("Europe")
elif info == "capital_city of France":
    print("Paris")
else:
     print("i dont know what your talking about")                              

part1()

If you are using a lot of data points, then it is probably better to have a list/database like you have though. Here's the quick and dirty fix if you want it though. (Also, if you use this method, you can convert the user input string into all lowercase using .lower I believe. This way, capitalization does not matter when inputting).

Upvotes: 1

Related Questions