Python: 사랑 계산기 + .lower() + count("")

Frigate·2022년 5월 27일
0

Python 기초문법

목록 보기
27/27

학습내용: 사랑계산기 제작, .lower(), .count("")
참고자료:https://replit.com/@appbrewery/day-3-5-exercise

1. 사랑계산기 제작해보기

  • 내 이름과 상대이름을 적고 TRUE,LOVE 글자를 적음
  • 두 사람 이름에 T,R,U,E,L,O,V,E 가 몇번 나오나 셈
  • TRUE 갯수는 10의자리, LOVE갯수는 1의 자리에 나타냄
  • 그것이 사랑하는 % 라는 계산기.

조건

  • LOVE 점수 Less than 10 or greater than 90 ⇒ "Your score is x, you go together like coke and mentos." 출력

  • Love 점수 40~50 사이 ⇒ "Your score is y, you are alright together." 출력

  • Otherwise(나머지의 경우) ⇒ "Your score is z." 출력

사랑계산기 예시

Arianna Rebolini

Channing Tatum

T:2 L:1

R:2 O:1

U:1 V:0

E:1 E:1

합:6 합:3 = 63%

* 사랑계산 과정 예시

name1 = "Angela Yu"

name2 = "Jack Bauer"

T occurs 0 times

R occurs 2 times

U occurs 2 times

Total = 5

L occurs 1 time

O occurs 0 time

V occurs 0 times

E occurs 2 times

Total = 3

Love Score = 53

Print: "Your score is 53."

2 3 3 2

* 실행결과 예시

* .lower() 은 문자열의 대문자를 모두 소문자로 바꿔줌

"Frigate".lower()
#실행결과: frigate

* .count("") 는 문자열에서 글자가 나오는 횟수를 알려줌

"Angela".count("a")  #괄호안의 세고자 하는 글자를 특정해줌
#실행결과: 1    #소문자 a만 세어줌
lower_case_name = "Angela".lower()
lower_case_name.count("a")
#실행결과: 2    # .lower() 점함수로 "Angela"를 모두 소문자로 변환해준 뒤, .count("a")함수로 소문자 a 갯수 셈

Try it!

  • if count_LOVE <10 or >90 : 이렇게 표현할 수 없고

if count_LOVE <10 or count_LOVE >90 : 이렇게 표현해야 함. 위의 >90의 비교대상을 명시해줘야함

  • 총TRUE 갯수의 합이 10의 자리, LOVE 갯수 합이 1의 자리므로, count_TRUE() 에 10을 곱할 것
# 🚨 Don't change the code below 👇
print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")
# 🚨 Don't change the code above 👆

#First *fork* your copy. Then copy-paste your code below this line 👇
#Finally click "Run" to execute the tests
count_TRUE = name1.lower().count("t") + name2.lower().count("t") + name1.lower().count("r") + name2.lower().count("r") + name1.lower().count("u") + name2.lower().count("u") + name1.lower().count("e") + name2.lower().count("e")

count_LOVE = name1.lower().count("l") + name2.lower().count("l") + name1.lower().count("o") + name2.lower().count("o") + name1.lower().count("v") + name2.lower().count("v") + name1.lower().count("e") + name2.lower().count("e")

if count_LOVE <10 or count_LOVE >90 :
  print(f"Your score is {10*count_TRUE+count_LOVE}, you go together like coke and mentos.")

elif count_LOVE >=10 and count_LOVE <=50 :
  print(f"Your score is {10*count_TRUE+count_LOVE}, you are alright together.")

else:
  print(f"Your score is {10*count_TRUE+count_LOVE}.")

또 다른 표현

  • 문자열은 concatenate(서로 이어지기) 가능함
print("Welcome to the Love Calculator!")
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")

combined_string = name1 + name2  #이름합해주기, 결국 모든 이름을 카운팅해야대므로..
lower_case_string = combine_string.lower() #소문자로 통일해주기

t = lower_case_string.count("t")
r= lower_case_string.count("r")
u = lower_case_string.count("u")
e = lower_case_string.count("e")

true = t+r+u+e  # 10의 자리 수 구함!

l = lower_case_string.count("l")
o = lower_case_string.count("o")
v = lower_case_string.count("v")
e = lower_case_string.count("e")

love = l+o+v+e

love_score = str(true) + str(love)    # 각 숫자를 이어서 2자릿수 만들기. true와 love는 정수임
                                    # 정수인 데이터 2개를 문자취급해서 10의자리 하나, 1의 자리 하나 출력되게할려면
                                       문자열로 형변환해서 하나로 이어줄 수 있음

int_score = int(love_score)   #이 구문 없으면 print오류뜸. 

if (love_score <10) or (love_score >90) :  #괄호는 가독성을 위함. 연산자 or은 둘중 조건하나만 참이면 실행결과True임
   print(f"Your love score is {love_score}, you go together like coke and mentos.")

elif (love_score>=40 and (love_score <=50) :   # 두조건이 모두 TRUE여야함
   print("Your score is {love_score}, you are alright together.")

else :
   print(f"Your score is {love_score}")

#실행결과: 오류 뜸. 이유는 love_score가 문자열이기 때문임. love_score = int(str(true) + str(love)) 이렇게 표현해줘야함
profile
Swift

0개의 댓글