Hello all. I’m trying to figure why visual studios says I need to define a variable. and where to define it. I have the whole code at GitHub - jamesboeck/MPG: a Mutiple application in one.
"for user_choice in user_choices:
" user_choice = user_choice.strip() # remove leading/trailing spaces
if user_choice in options:
"optionsuser_choice""
I’m quoting more of your code for context.
def main_new():
options = {
'1': calculate_travel_time,
'2': main,
'3': calculate_gas_mileage_main
}
while True:
print('1: Calculate travel time')
print('2: Calculate MPG for your trip')
print('3: Calculate the effect of temperature on MPG')
user_choices = input('Enter the numbers corresponding to your choices, separated by commas: ').split(',')
for user_choice in user_choices:
user_choice = user_choice.strip() # remove leading/trailing spaces
if user_choice in options:
optionsuser_choice
else:
print(f"Invalid choice: {user_choice}. Please enter a valid number.")
The variable optionsuser_choice is not defined in your code. It looks like you are trying to call a function stored in the options dictionary.
To do that replace
optionsuser_choice
with
chosen_function = options[user_choice] # get the function
chosen_function() # call the function
This can be more succinctly written as
options[user_choice]()