Python Forum
name.split() + (And) + print question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
name.split() + (And) + print question
#1
The problem:

Input = My name is Bob and this is my wife Alice.
Output = Hello Bob and Alice!
else
Input = My name is Bob.
Output = Hello Bob!
else
Input = My name is Bob.
Output = Hello Bob!


What i came up with:

name = (input())
for name in name.split():

if name == ('Bob') and ('Alice'):
    print ('Hello Bob & Alice!')
elif name == ('Alice'):
    print ('Hello Alice!')
elif name == ('Bob'):
    print ('Hello Bob!')
I tried the >>> in operator but the problem is that it says its true if input was (Bobdylan)

I feel like i am close

Would love if someone had the time to do the full code, its easier for me to understand then getting an explanation
Reply
#2
Hello,
name = input()

if 'Alice' in name:
    if 'Bob' in name:
        print ('Hello Bob & Alice!')
    else:
        print ('Hello Alice!') 
elif 'Bob' in name:
    print ('Hello Bob!')
sklord likes this post
I speak Python but I don't speak English (I just read it a little). If I express myself badly, please blame the translator^^.
Reply
#3
(Mar-25-2022, 03:11 PM)Coricoco_fr Wrote: Hello,
name = input()

if 'Alice' in name:
    if 'Bob' in name:
        print ('Hello Bob & Alice!')
    else:
        print ('Hello Alice!') 
elif 'Bob' in name:
    print ('Hello Bob!')

Thank you so much! Makes much more sense now...
Reply
#4
That is not how I would write this. That would work for Bob and Alice and nothing else. Do you notice anything about "Bob" and "Alice" that is different from "my", "name", "is", "my", "wife"?
Reply
#5
Hello,
@deanhystad: What do you have in mind? Show it.
If for you the difference is the difference between words and name is the capitalization it is not a valid criterion ...
I speak Python but I don't speak English (I just read it a little). If I express myself badly, please blame the translator^^.
Reply
#6
Hey!
so i tried messing around with the code, but the problem that i got too is lack of options.

I am trying to understand my options and limitations with Logic operators


If i add in a third name the problem becomes obvious
name = ('A B C')

if 'A' in name:
    print ('A')
    if 'B' in name: 
        print (' B')
        if 'C' in name:
            print ('C')
        else:
            print ('AB')     
if 'B' in name:
    print ('B')
    if 'C' in name:
        print (' C')
else:
    print ('Stranger')
I am trying to output
A B C
A C
B C
B A
Stranger


Maybe i should use Booleans instead? or what would the options be?
Reply
#7
Hello,
I propose this:
entry = input(">").split()

d = {"A": False, "B": False, "C": False}
for word in entry:
    if 'A' == word:
        d['A'] = True
    if 'B' == word:
        d['B'] = True
    if 'C'== word:
        d['C'] = True
    
out = (key for key in d if d[key])
print(*out)
    

        
I speak Python but I don't speak English (I just read it a little). If I express myself badly, please blame the translator^^.
Reply
#8
Really nice, gave me some code to think about! Also what is to meaning to have input(">") rather then input()

EDIT: Stupid question, i understand now...
Reply
#9
Quote:Also what is to meaning to have input(">") rather then input()
It's all the same. It's just to display ">".
I speak Python but I don't speak English (I just read it a little). If I express myself badly, please blame the translator^^.
Reply
#10
Your latest question is how do you print "Hello Bob!" or "Hello Bob & Alice!" depending on if you have 1 name or 2 names (or "Hello Stranger!" if there are no names). Or am I way off base?

Assuming I am correct, are you allowed to use str.join()? That might be helpful.

You do not need to cover all possible combinations in your logic. You only have three possibilities:
If no names print "Hello Stranger!"
If one name print "Hello name!" replacing name with the actual name.
if more than one name print "Hello name0 and name1!" replacing name0 and name1 with actual names. This last case can be extended to any number of names > 1.

Collecting the names can be easily done if you know all the possible names.
names = []
for word in input("Hello! ").split():
    if word in ("Bob", "Alice"):
        names.append(word)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question on "define function"; difference between return and print extricate 10 4,598 Jun-09-2020, 08:56 PM
Last Post: jefsummers
  Self Paced Web course - split ip and print each piece in binary and hex sumncguy 2 2,240 Dec-04-2019, 06:03 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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