어떻게 컴퓨터 과학자 대화 판처럼 생각하고, 연습 중 하나가 다음 요구 사항이하려면 사용하여 파이썬을 배우고 :내 파이썬 코드에 어떤 문제가 있는지 파악하는 데 도움이 필요
"에 변수에 할당을하여 예를 들어시, 연설, 케이크 구우기 지침, 영감을주는 구절 등을 포함하는 삼중 인용 문자열을 프로그래밍하십시오.
영문자 수를 계산하는 함수를 작성하십시오 z 또는 A부터 Z까지)를 입력 한 다음 문자 'e'가 얼마나 많은지 추적합니다. 함수는 다음과 같이 텍스트 분석을 인쇄해야합니다 :
텍스트에 243 자의 알파벳 문자가 포함되어 있으며 그 중 109 개 (44.8 %)는 'e'입니다. "
나는 (내가) 묻는 질문을 정확히하는 것처럼 보이지만 코드를 테스트 할 때 솔루션을 점검 할 때 결과가 달라진다.
내 코드 :
text = ''' "If the automobile had followed the same development cycle as the computer, a
Rolls-Royce would today cost $100, get a million miles per gallon, and explode
once a year, killing everyone inside."
-Robert Cringely'''
lowercase_text = text.lower()
def charCounter(some_text):
e_counter = 0
char_counter = 0
for char in lowercase_text:
if char == 'e':
e_counter = e_counter + 1
else:
char_counter = char_counter + 1
return ("Your text contains " + str(char_counter) + " alphabetic characters, of which " + str(e_counter) + " (" + str((e_counter/char_counter) * 100) + "%)" + "are 'e'.")
내 코드 출력 : 저자 솔루션이
def count(p):
lows="abcdefghijklmnopqrstuvwxyz"
ups="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numberOfe = 0
totalChars = 0
for achar in p:
if achar in lows or achar in ups:
totalChars = totalChars + 1
if achar == 'e':
numberOfe = numberOfe + 1
percent_with_e = (numberOfe/totalChars) * 100
print("Your text contains", totalChars, "alphabetic characters of which", numberOfe, "(", percent_with_e, "%)", "are 'e'.")
p = '''"If the automobile had followed the same development cycle as the computer, a
Rolls-Royce would today cost $100, get a million miles per gallon, and explode
once a year, killing everyone inside."
-Robert Cringely'''
count(p)
코드 출력 : 저자가 제공하는
Your text contains 188 alphabetic characters, of which 25 (13.297872340425531%)are 'e'.
솔루션 코드
Your text contains 166 alphabetic characters of which 25 (15.060240963855422 %) are 'e'.
누군가 내가 뭘 잘못하고 있는지 설명해 주시겠습니까? 왜 결과에 이러한 차이가 있는지 나는 알지 못합니다.
스택 오버플로에서 게시물 서식을 지정하는 방법에 대해 알아보십시오. –