I made a sample below to help explain. The problem with an if/else clause is that it makes the variables local so i can't assign a returned value under if: when the user enters 'y' and use it across the bounds of else: -See the value return_from_add and the two places I want to use it. I want to obey running my code sequentially and simply have the user enter a 'n' instead of a 'y' to run a further down piece of code. I don't know how without an if/else statement maybe somebody else has figured out something similar before and then you would understand my frustration in even wording the question appropriately. Because what else comes to mind other than **if **i want to do this **else ** do this for a yes no question.
def run_main_prgm_2():
while True:
other = 'Invalid Response'
no = 'n'
yes = 'y'
y_n = input('Would you like to add a new person to the address book? y/n \n - ')
if y_n == 'y':
return_from_add = add_person()
return yes
elif y_n = 'n':
search_full_name1 = input("Type in a fullname for indexing\n- ")
index_person(return_from_add,search_full_name1)
return no
else:
return other
If you're having trouble understanding my question look at where return_from_add is being used in two places where it's not possible because the if elif makes it local even under the same function. Which is why I included a def at the top to illustrate that this is all under the same function. Now how can i pull this off with local variables under the same function
I've tried messing with while loops. Basically it would be cool in programming if there was something that said skip x number of lines and run this instead when the user enters a specific key.
CodePudding user response:
There is a syntax error in the code. In the second if statement, the 'y_n' variable is compared to the string 'y' using a single equal sign '=' instead of a double equal sign '=='. This will cause a syntax error, as the single equal sign is used to assign a value to a variable, while the double equal sign is used to compare two values.
CodePudding user response:
The answer is to use global variables. In this example I included examples for the functions add_person and index_person so that the answer can compile.
return_from_add = {'Harry' :
{'name' : 'Harry James Potter',
'phone' : 'n/a',
'address' : '4 Pivet Drive Little Whinging, Surrey'}}
def add_person():
#CREATE A TEST DICTIONARY VARIABLE TO STORE ADDRESSES
example_dictionary = {'Harry' :
{'name' : 'Harry James Potter',
'phone' : 'n/a',
'address' : '4 Pivet Drive Little Whinging, Surrey'}}
return example_dictionary
# Use the fullname or nickname to display the searched for address.
def index_person(index,fname):
for person in index:
if person.lower() == fname.lower() or index[person]['name'].lower() == fname.lower():
print('\nName: %s\nPhone Number: %s\nAddress: %s\n' %(index[person]['name'], index[person]['phone'], index[person]['address']))
return index
else:
print('\n\n-No match\n')
def main():
run_main_prgm_2()
def run_main_prgm_2():
while True:
other = 'Invalid Response'
no = 'n'
yes = 'y'
y_n = input('Would you like to add a new person to the address book? y/n \n - ')
if y_n == 'y':
global return_from_add
return_from_add = add_person()
return yes
elif y_n == 'n':
search_full_name1 = input("Type in a fullname for indexing\n- ")
index_person(return_from_add,search_full_name1)
return no
else:
return other
#Call the main function if exists.
if __name__=='__main__':
main()