Python入门简单问题两个

Python入门简单问题两个

问题描述:

问题1:
写一个函数交集(str1, str2),返回str1和str2的交集。(交集意味着str2中的一个字符出现在str1中的某个位置。)每个字母在输出中最多只能表示一次。
字母应该按照它们在第一个字符串中出现的顺序排列。您应该使用for循环来遍历第一个字符串。

eg intersection("hello","hello") -> helo

问题2:
编写一个函数get_names(),提示用户输入自己的名字,然后返回一个包含用户名和姓氏的元组。必须使用str.partition分隔用户的姓和名。必须将str.partition的结果“解压”到单独的变量中。您可以假设用户只输入了两个名称。
这个我都没看懂··
拜托大佬们了!!

代码:

def intersection(s1,s2):
    result=""
    for i in s2:
        for j in s1:
            if i==j:
                c=0
                for k in result:
                    if k==i:
                        c+=1
                if c==0:
                    result+=i
    print(result)
    return result

intersection("hello","hello")

def get_names():
    name=input("input name:")
    v=name.partition(".")
    first_name=v[0]
    last_name=v[2]
    print("first name is ",first_name)
    print("last name is ",last_name)

get_names()


运行效果:
图片说明