Beginner - Assistance please - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Beginner - Assistance please (/thread-31066.html) |
Beginner - Assistance please - Elippsis007 - Nov-20-2020 Hello! I am really struggling to understand why I am getting a return on my interpreter 3 lines for my ['yes, 'no'] prompt. Would someone kindly review my code and assist me on it? It's driving me nuts here OS - Windows Text Editor - Sublime Text Interpreter - Windows PowerShell(x86) -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- reservation_name = input("\nGood afternoon, Anthony's dinner reservations. " "May I have your full name please? ") print(f"\nHello {reservation_name.title()} are you looking to make a dinner reservation with us? ") reservation_reply = ['yes', 'no'] reservation_prompt = input("-> ") for reservation_reply in reservation_prompt: if reservation_reply == 'yes': print("\nHow many dining guests can I book you in for?") else: print("\nOh this line is for reservations only, please hold the line and I will forward you on to our management team.")
RE: Beginner - Assistance please - perfringo - Nov-20-2020 You should try it with 'no' and it will print twice. Why? You iterate over answer characterwise: for reservation_reply in reservation_prompt: # i.e.: for char in reservation_prompt:As no character match 'yes' else suite is executed by number of iterations over user input. Can be illustrated as: >>> reservation_prompt = 'yes' >>> for reservation_reply in reservation_prompt: ... print(reservation_reply) ... y e s RE: Beginner - Assistance please - Elippsis007 - Nov-20-2020 (Nov-20-2020, 02:55 PM)perfringo Wrote: You should try it with 'no' and it will print twice. Why? You iterate over answer characterwise: Thank you very much for your reply! Honestly I really appreciate this. So what you are saying is, the reason the return prints 3 times for "yes" is because of the characters? And "no" prints out two times because of the characters. How can I get one return for "yes" and for the "else" the return for "no" Basically a return of one line... not multiple.. Many thanks! RE: Beginner - Assistance please - sandeep_ganga - Nov-20-2020 "for" will iterate over the given, thus try use "if" to direct compare and see if that helps reservation_name = input(f"\nGood afternoon, Anthony's dinner reservations. \n May I have your full name please? ") print(f"\nHello {reservation_name.title()} are you looking to make a dinner reservation with us? ") reservation_reply = ['yes', 'no'] reservation_prompt = input("-> ") if reservation_prompt.lower() in reservation_reply: if reservation_prompt.lower() == 'yes': print("\nHow many dining guests can I book you in for?") else: print("\nOh this line is for reservations only, please hold the line and I will forward you on to our management team.")Best Regards, Sandeep. GANGA SANDEEP KUMAR RE: Beginner - Assistance please - Elippsis007 - Nov-20-2020 (Nov-20-2020, 03:26 PM)sandeep_ganga Wrote: "for" will iterate over the given, thus try use "if" to direct compare and see if that helps Sandeep!!!! You did it! thank you so so much! I never realized I could use If and If... for is to loop...right? Thank you so much for your help! Really!!! I am very grateful! |