Python Forum
Beginner - Assistance please
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner - Assistance please
#1
Big Grin 
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 Big Grin

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.")
Error:
Good afternoon, Anthony's dinner reservations. May I have your full name please? michael drumm Hello Michael Drumm are you looking to make a dinner reservation with us? -> yes Oh this line is for reservations only, please hold the line and I will forward you on to our management team. Oh this line is for reservations only, please hold the line and I will forward you on to our management team. Oh this line is for reservations only, please hold the line and I will forward you on to our management team.
Reply
#2
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
Elippsis007 likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(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:

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


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!
Reply
#4
"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
Elippsis007 likes this post
Reply
#5
(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

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

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! Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner Assistance liamwemyss 7 5,992 Jun-08-2017, 01:51 PM
Last Post: liamwemyss

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020