Python Forum
Python coding mutiple elif's
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python coding mutiple elif's
#1
Newbie to Python.
Got this code from this forum.Tried extending it but it is not working.

Code I am running:
import random
greetings = ['hola', 'hello', 'hi', 'Hi', 'hey!','hey']

question = ['How are you?','How are you doing?']
responses = ['Okay',"I'm fine"]

question1 = ['Your name please?','Can I have your name?']
responses1 = ['My name is Steve',"Steve"]

question2 = ['Your age please?','Can I have your age?']
responses2 = ['My age is 24',"24"]

while True:
        userInput = input(">>> ")
        if userInput in greetings:
                random_greeting = random.choice(greetings)
                print(random_greeting)
        elif userInput in question:
                random_response = random.choice(responses)
                print(random_response)
        elif userInput in question1:
                random_responses1 = random.choice(responses1)
                print(random_responses1)
        elif userInput in question2:
                random_responses2 = random.choice(responses2)
                print(random_responses2)
        else:
                print("I did not understand what you said")
Output:
Output:
hola Hi How are you doing? I'm fine Your name please? I did not understand what you said <-------- Its not printing --- 'My name is Steve' OR "Steve"
Can I get help.
Thanks
Reply
#2
(Aug-02-2019, 10:12 AM)metro17 Wrote: Its not printing --- 'My name is Steve' OR "Steve"
You must not of put what you said you put in (there isnt even the >>> for your prompt in your output).

This is what i get
Output:
>>> hola hey! >>> How are you doing? I'm fine >>> Your name please? Steve
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help replacing word in Mutiple files. (SOLVED) mm309d 0 795 Mar-21-2023, 03:43 AM
Last Post: mm309d
  python ast if-elif confusion holyghost 3 3,016 Apr-19-2020, 12:36 PM
Last Post: pyzyx3qwerty
  simple if elif conditions in Python Chandan 2 2,027 Jan-08-2020, 02:36 PM
Last Post: DeaD_EyE
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,214 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  if-elif-else coding help esyfilamaj 6 3,647 Oct-30-2018, 11:45 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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