I am working on a script to easily determine the waterfowl limits of a state by typing its name. I am also using the pyinputplus module for input validation, but I am hung up on one important step.
# Function of the code is to input name of central flyway state (waterfowl hunting), and print the limit of ducks and geese for that state.
# Please see following link for more information: https://www.ducks.org/conservation/where-ducks-unlimited-works/waterfowl-migration-flyways/du-projects-central-flyway
# Imports pyinputplus for input validation
import pyinputplus as pyip
# Dictionary of Central Flyway States and their respective limits
Montana = {
"State:": "Montana",
"Duck Limit:": "7",
"Goose Limit:": "6",
"Waterfowl Season:": "Oct. 2 - Jan. 14",
}
Wyoming = {
"State:": "Wyoming",
"Duck Limit:": "7",
"Goose Limit:": "3",
"Waterfowl Season:": "Sept. 26 - Jan. 8",
}
Colorado = {
"State:": "Colorado",
"Duck Limit:": "6",
"Goose Limit:": "3",
"Waterfowl Season:": "Oct. 11 - Dec. 1 and Dec. 13 - Jan. 25",
}
NM = {
"State:": "New Mexico",
"Duck Limit:": "6",
"Goose Limit:": "5",
"Waterfowl Season:": "Oct. 4 - Jan. 2",
}
Texas = {
"State:": "Texas",
"Duck Limit:": "6",
"Goose Limit:": "5",
"Waterfowl Season:": "Nov. 13 - Nov. 28 and Dec. 4 - Jan. 30",
}
Oklahoma = {
"State:": "Oklahoma",
"Duck Limit:": "6",
"Goose Limit:": "8",
"Waterfowl Season:": "Oct. 17 - Jan. 13",
}
Kansas = {
"State:": "Kansas",
"Duck Limit:": "6",
"Goose Limit:": "6",
"Waterfowl Season:": "Nov 1. - Jan. 4 and Jan. 17 - Jan. 25",
}
Nebraska = {
"State:": "Nebraska",
"Duck Limit:": "6",
"Goose Limit:": "5",
"Waterfowl Season:": "Oct. 3 - Dec. 15 and Jan. 6 - Jan. 27",
}
SD = {
"State:": "South Dakota",
"Duck Limit:": "6",
"Goose Limit:": "3",
"Waterfowl Season:": "Oct. 11 - Jan. 15",
}
ND = {
"State:": "North Dakota",
"Duck Limit:": "6",
"Goose Limit:": "8",
"Waterfowl Season:": "Sept. 27 - Dec. 7",
}
# Asking user to type name of state
print(
"""Please type Name of Central Flyway State for waterfowl information.
"""
)
# pyip module with a whitelist of available state names. Will return error if input is not whitelisted.
result = pyip.inputMenu(
[
"Montana",
"Wyoming",
"Colorado",
"New Mexico",
"Texas",
"Oklahoma",
"Kansas",
"Nebraska",
"South Dakota",
"North Dakota",
],
)
# Function to return the result of inputMenu and call out the corresponding dictionary with it's key:value pairs
def statefunction(result):
print(result)
if statefunction(result) == "Montana":
print(Montana.items)
elif statefunction(result) == "Wyoming":
print(Wyoming.items)
elif statefunction(result) == "Colorado":
print(Colorado.items)
elif statefunction(result) == "New Mexico":
print(NM.items)
elif statefunction(result) == "Texas":
print(Texas.items)
elif statefunction(result) == "Oklahoma":
print(Oklahoma.items)
elif statefunction(result) == "Kansas":
print(Kansas.items)
elif statefunction(result) == "Nebraska":
print(Nebraska.items)
elif statefunction(result) == "South Dakota":
print(SD.items)
elif statefunction(result) == "North Dakota":
print(ND.items)
I would like for the code to iterate through the 'if' and 'elif' statements and print the items for the corresponding dictionaries. For example, if I type "Texas", into the inputMenu when prompted, I want the dictionary key:value pairs for Texas to print to the screen. Right now, all that happens is I type in "Texas", or any other whitelisted string under the inputMenu, and the code finishes.
CodePudding user response:
You have done everything correctly, but just need to parse the state
as an argument
into the function and also call the function.
def statefunction(state):
print(state)
statefunction(state)
This will work.
CodePudding user response:
I have added a second answer here. Here we use the sys
module and the getattr
function to convert strings
from the input to dicts
that you defined in the code.
import sys
# Dictionary of Central Flyway States and their respective limits
Montana = {
"State:": "Montana",
"Duck Limit:": "7",
"Goose Limit:": "6",
"Waterfowl Season:": "Oct. 2 - Jan. 14",
}
Wyoming = {
"State:": "Wyoming",
"Duck Limit:": "7",
"Goose Limit:": "3",
"Waterfowl Season:": "Sept. 26 - Jan. 8",
}
Colorado = {
"State:": "Colorado",
"Duck Limit:": "6",
"Goose Limit:": "3",
"Waterfowl Season:": "Oct. 11 - Dec. 1 and Dec. 13 - Jan. 25",
}
NM = {
"State:": "New Mexico",
"Duck Limit:": "6",
"Goose Limit:": "5",
"Waterfowl Season:": "Oct. 4 - Jan. 2",
}
Texas = {
"State:": "Texas",
"Duck Limit:": "6",
"Goose Limit:": "5",
"Waterfowl Season:": "Nov. 13 - Nov. 28 and Dec. 4 - Jan. 30",
}
Oklahoma = {
"State:": "Oklahoma",
"Duck Limit:": "6",
"Goose Limit:": "8",
"Waterfowl Season:": "Oct. 17 - Jan. 13",
}
Kansas = {
"State:": "Kansas",
"Duck Limit:": "6",
"Goose Limit:": "6",
"Waterfowl Season:": "Nov 1. - Jan. 4 and Jan. 17 - Jan. 25",
}
Nebraska = {
"State:": "Nebraska",
"Duck Limit:": "6",
"Goose Limit:": "5",
"Waterfowl Season:": "Oct. 3 - Dec. 15 and Jan. 6 - Jan. 27",
}
SD = {
"State:": "South Dakota",
"Duck Limit:": "6",
"Goose Limit:": "3",
"Waterfowl Season:": "Oct. 11 - Jan. 15",
}
ND = {
"State:": "North Dakota",
"Duck Limit:": "6",
"Goose Limit:": "8",
"Waterfowl Season:": "Sept. 27 - Dec. 7",
}
# Asking user to type name of state
print(
"""Please type Name of Central Flyway State for waterfowl information.
"""
)
def str_to_dict(my_string):
''' convert string to dict '''
return getattr(sys.modules[__name__], my_string)
state = input('add your state: ')
state_dict = str_to_dict(state)
def statefunction(s):
print(s)
statefunction(state_dict)
You could do checking in the state input string too...
The output is this:
add your state: Montana
{'State:': 'Montana', 'Duck Limit:': '7', 'Goose Limit:': '6', 'Waterfowl Season:': 'Oct. 2 - Jan.
14'}