繁体   English   中英

这段代码是一个爱计算器的代码我怎样才能简化代码?

[英]This code is a love calculator code how can I simplify the code?

首先我想 state 表明我是一名中学生。 这个代码是当用户输入两个名字时,它可以看到名字中有多少个“真爱”字母,看看他们进展得如何。 我想知道如何简化此代码,如下所示。 有人可以帮助我吗?

def love_calculator():
    name1=str(input("please enter your name:"))
    name2=str(input("please enter your name:"))
    word1 = ['t', 'r', 'u', 'e']
    word2 = ['l','o','v','e']
    count1=0
    count2=0
    for i in name1:
        if i in word1:
            count1=count1+1
    for i in name2:
        if i in word1:
            count1=count1+1
    for i in name1:
        if i in word2:
            count2=count2+1
    for i in name2:
        if i in word2:
            count2=count2+1
    score= int(str(count1) + str(count2))
    if score<10 or score > 90:
        print("Your score is", score,  "you go together like coke and mentos.")
    elif score > 40 and score < 50:
        print("Your score is", score, "you are alright together.")
    else:
        print("your score is", score)


love_calculator()

您可以获得count1count2作为

count1 = len([x for x in name1+name2 if x in "true"])
count2 = len([x for x in name1+name2 if x in "love"])
def love_calculator():
    name1 = str(input("please enter your name:"))
    name2 = str(input("please enter your name:"))
    word1 = "true"
    word2 = "love"
    count1 = 0
    count2 = 0
    combined_name = name1 + name2 
    for symbol in combined_name: 
        if symbol in word1: 
            count1 += 1 
        if symbol in word2: 
            count2 += 1
    score = int(str(count1) + str(count2))
    if score<10 or score > 90:
        print("Your score is", score,  "you go together like coke and mentos.")
    elif score > 40 and score < 50:
        print("Your score is", score, "you are alright together.")
    else:
        print("your score is", score)


love_calculator()

您可以将具有两个名称的变量组合成一个变量。 然后只使用一个 for 循环来传递这两个名称。 另外,不要忘记遵守 PEP 的注册规则

这是一个更短的版本

def love_calculator():
    name1=str(input("please enter your name:"))
    name2=str(input("please enter your name:"))
    word = 'truelov'
    count1=0
    count2=0
    
    count1 = sum( [1 for i in name1 if i in word] )
    count2 = sum( [1 for i in name2 if i in word] )
    
    score= int(str(count1) + str(count2))
    
    if score<10 or score > 90:
        print("Your score is", score,  "you go together like coke and mentos.")
        
    elif score > 40 and score < 50:
        print("Your score is", score, "you are alright together.")
        
    else:
        print("your score is", score)


love_calculator()

按照相同的逻辑计算count1count2 ,您可以在 for-iteration 中组合这两个名称

def love_calculator():
    name1 = str(input("please enter your name:"))
    name2 = str(input("please enter your name:"))
    word1, word2 = list('true'), list('love')
    count1, count2 = 0, 0
    for i in name1+name2:
        if i in word1:
            count1 += 1
    for i in name1+name2:
        if i in word2:
            count2 += 1
    score = int(str(count1) + str(count2))
    if score<10 or score>90:
        print("Your score is", score, "you go together like coke and mentos.")
    elif score>40 and score<50:
        print("Your score is", score, "you are alright together.")
    else:
        print("your score is", score)

love_calculator()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM