![]() |
name.split() + (And) + print question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: name.split() + (And) + print question (/thread-36746.html) Pages:
1
2
|
name.split() + (And) + print question - sklord - Mar-25-2022 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 RE: name.split() + (And) + print question - Coricoco_fr - Mar-25-2022 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!') RE: name.split() + (And) + print question - sklord - Mar-25-2022 (Mar-25-2022, 03:11 PM)Coricoco_fr Wrote: Hello, Thank you so much! Makes much more sense now... RE: name.split() + (And) + print question - deanhystad - Mar-25-2022 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"? RE: name.split() + (And) + print question - Coricoco_fr - Mar-26-2022 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 ... RE: name.split() + (And) + print question - sklord - Mar-26-2022 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? RE: name.split() + (And) + print question - Coricoco_fr - Mar-26-2022 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) RE: name.split() + (And) + print question - sklord - Mar-26-2022 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... RE: name.split() + (And) + print question - Coricoco_fr - Mar-26-2022 Quote:Also what is to meaning to have input(">") rather then input()It's all the same. It's just to display ">". RE: name.split() + (And) + print question - deanhystad - Mar-26-2022 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) |