2017-11-12 4 views
0

저는 프로그래밍에 초보자이기도합니다. 나는 여기에 몇 가지 주제의 도움으로 BMI를 계산하는 프로그램을 작성했습니다.효과적인 프로그래밍으로 Python에서 BMI를 계산하십시오.

코드 검토 : 프로그램에서 주 기능을 3 번 사용했고 bmi 수식을 쓰는 것이 좀 더 효율적일 수 있다고 생각합니다. 즉, 부동 소수점을 변수에 캐스트하지 않아도됩니다. 동일한 코드를 효율적으로 작성하시기 바랍니다.

또한 https://automatetheboringstuff.com에서 ptyhon을 배우고 있습니다. 가장 좋은 방법은 python 온라인을 배우는 것이 좋습니다. 명령 줄을 사용하여

  • 코드의 일부 문서를 넣어
  • 전환 (전역 변수를 제거하는 데 도움이)

    import re 
    print('Calculate your body mass index(BMI) today.') 
    def main(): 
    
        while True: 
         myName = input('Enter your name:') 
         if re.match('^[a-z, A-Z, ,]*$', myName): 
          print ('Nice to meet you, ' + myName) 
          return 
         else: 
          print ('You should enter a to z letters only!') 
    
    if __name__ == "__main__": 
        main() 
    def main(): 
        while True: 
         global height 
         height = input('Enter your height in cms: ') 
         if re.match('^[0-9]*$', height): 
          return 
         else: 
          print ('You should enter 0 to 9 letters only!') 
    
    if __name__ == "__main__": 
        main() 
    def main(): 
        while True: 
         global weight 
         weight = input('Enter your weight in kgs: ') 
         if re.match('^[0-9]*$', weight): 
          return 
         else: 
          print ('You should enter 0 to 9 letters only!') 
    
    if __name__ == "__main__": 
        main() 
    bmi = round((float (weight)/ (0.0001*float(height)*float(height))), 2) 
    minIdealWeight = round(18.5*0.0001*float(height)*float(height), 2) 
    maxIdealWeight = round(25*0.0001*float(height)*float(height), 2) 
    if 18.5<bmi<25: 
         print('Your BMI is '+str(bmi)+'.' '\n Maintain your weight, you\'re normal.') 
    elif bmi<18.5: 
         print('Your BMI is '+str(bmi)+'.' '\nGain some weight, you\'re underweight.') 
         print('Your ideal weight is between '+str(minIdealWeight)+' to ' +str(maxIdealWeight)+' kgs.') 
    else: 
         print('Your BMI is '+str(bmi)+'.' '\nLoose some weight, you\'re overweight.') 
         print('Your ideal weight is between '+str(minIdealWeight)+' to ' +str(maxIdealWeight)+' kgs.') 
    
  • +0

    StackOverflow에 오신 것을 환영합니다. [좋은 질문을하는 법] (https://stackoverflow.com/help/how-to-ask)을보십시오. 귀하의 현재 질문은 다소 모호합니다. 특정 효율 요건은 무엇입니까? 벤치마킹 데이터 및 대상을 표시하고 지금까지 시도한 내용도 표시하십시오. 그런면에서 "효율적인"이란 무엇을 의미합니까? 일반적으로 질문을 최대한 구체적으로 작성하십시오. 그것은 좋은, 신속한 대답을 얻는 기회를 극대화합니다. 모든 것을 말하면서이 질문은 [CodeReview] (https://codereview.stackexchange.com/)에서 더 나을 수 있습니다. –

    +0

    방금 ​​프로그래밍을 배우기 시작했다면, 더 쉬운 것으로 시작하는 것이 좋습니다. 당신이 사용하고있는 사이트는 괜찮습니다.하지만 한 걸음 한발 한발 내딛으십시오. 아주 기본적인 것을 마스터하기 전에 더 복잡한 것들로 뛰어 들지 마십시오. 정규 표현식은 기초 이상입니다! –

    답변

    0

    난 당신이 클래스를 사용

    • 볼 제안 : 다음은 내 코드입니다
    • 일반적인 오류 처리 기능이 있습니다.

    그런 말을 끝내고 잘하고 일을 계속하십시오.

    관련 문제