簡體   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