I created this function and want to call the returned result but, I'm not sure how to get the variable back. If possible I'd also like a different message to pop up if the user types n. Could anyone help?
def give_entertainment():
random_form_of_entertainment = random.choice(form_of_entertainment)
good_user_input = "y"
while good_user_input == "y":
user_input = input(f"We have chosen {random_form_of_entertainment} for your entertainment! Sound good? y/n: ")
if good_user_input != user_input:
random_form_of_entertainment = random.choice(form_of_entertainment)
continue
else:
print("Awesome! Glad we got that figured out. Your trip is all planned! ")
return random_form_of_entertainment
CodePudding user response:
x = give_entertainment()
Should store the return of your function into x
.
With:
print(x)
you should see what's stored in your x
variable.
CodePudding user response:
Call the method while assigning it to a variable.
some_var = your_method()
Now this some_var
variable have the returning value.
CodePudding user response:
I'd personally use a recursive function for this, I made it work with y/n/invalid_input cases. Also with this an invalid input won't update random_form_of_entertainment so the user has to say y or n for it to change. I hope this is what you're looking for!
def give_entertainment():
random_form_of_entertainment = random.choice(forms_of_entertainment)
good_user_input = "y"
bad_user_input = "n"
invalid_input = True
while invalid_input:
user_input = input(f'We have chosen {random_form_of_entertainment} for your entertainment! Sound good? y/n: ')
if user_input == good_user_input:
print("Awesome! Glad we got that figured out. Your trip is all planned for!")
return random_form_of_entertainment
elif user_input == bad_user_input:
print("Sorry to hear that, let me try again...")
return give_entertainment()
else:
print("I don't recognize that input, please try again.")
continue
chosen_result = give_entertainment()
print(f'You chose {chosen_result}!')