2017-12-10 3 views
1

모두 안녕하세요! 내 목표는 male_cal 또는 fem_cal (암시 적)이 올바르게 인쇄되도록 나이를 반복하는 것입니다. 도와주세요! 많은 감사 -> Pythonidaer < - 나는 섹스를했던 것처럼잘못된 사용자 입력을 방지하기 위해 어떻게 루프합니까?

print("\nWelcome to the Daily Caloric Intake Calculator!") 

나는 방법 루프 나이를 모른다.

age = int(input("\nHow old are you in years? ")) 

sex = input("\nAre you a male or a female? Enter 'male' or 'female'. ").lower() 
if sex == "female" or sex == "f": 
    sex = "female" 
elif sex == "male" or sex == "m": 
    sex = "male" 
else: 
    sex = input("Sorry, there's only two choices: MALE or FEMALE. ").lower() 

방정식에는 연령이 정수 여야합니다. 내가 어찌 할 수 있지?

height = float(input("\nHow tall are you in inches? ")) 
metric_height = float(height * 2.54) 
weight = float(input("\nWhat is your weight in pounds? ")) 
metric_weight = int(weight * 0.453592) 


activity_level = float(input(""" 
Please select your activity level: 

Sedentary (enter '1.2') 
Moderatively Active (enter '1.3') 
Active? (enter '1.4') 

""")) 

male_cal = 10 * metric_weight + 6.25 * metric_height - 5 * age - 161 
fem_cal = 10 * metric_weight + 6.25 * metric_height - 5 * age + 5 

if (sex == "male"): 

carbs = int(male_cal * .45) 
protein = int(male_cal * .20) 
fats = int(male_cal * .35) 
print("\nYour DCI should be: ", int(male_cal), "calories a day.") 
print(f"""\nThat means getting: 
{carbs} cals from carbs, 
{fats} cals from fats, and 
{protein} cals from protein.""") 
elif (sex == "female"): 

carbs = int(fem_cal * .45) 
protein = int(fem_cal * .20) 
fats = int(fem_cal * .35) 
print("\nYour DCI should be: ", int(fem_cal), "calories a day.") 
print(f"""\nThat means getting: 
{carbs} cals from carbs, 
{fats} cals from fats, and 
{protein} cals from protein.""") 
+3

트릭을 할해야합니다. –

+0

'{}'버튼을 사용하여 형식 코드를 수정하십시오. – furas

+0

안녕하세요 @TerryJanReedy 의견을 보내 주셔서 감사합니다. 너뿐만 아니라 너에게 고마워. 나는 SO 및 Reddit에 대한 올바른 형식을 연구 중이므로 가장 간결한 방식으로 질문 할 수 있습니다. 또한 나는 전반적으로 나의 연구 기술을 연구하고 있으므로, 이미 응답 된 질문을 앞으로하지는 않습니다. 그러나 확실하지 않으면 내 질문에 대한 답을 얻는 편이 잘못 될 것입니다. happy holidays :) – Pythonidaer

답변

0
while True: 
    try: 
     age = int(input("\nHow old are you in years? ")) 
     break 
    except ValueError: 
     print('please put in a number') 

이 몇 가지 유사한 질문이 완료 답변을 이미 있습니다

+3

나이가 설정되어 있는지 확인하려면 while 루프에서 코드를 래핑해야합니다. –

+0

당신 말이 맞아요. – Lyux

+0

@Lyux 도움에 너무 감사합니다! 나는 이것을 시도하고 완벽하게 작동했습니다. 그러나 나는 단지 답을 원한다. 나는 코드가하는 일을 이해하고 싶다. 그래서 앞으로 같은 기술을 요구할 필요가 없다. 내 첫 번째 게시물은 나를 매우 행복하게 만들었지 만 다시 질문하기 전에 올바른 질문을하는 방법을 알아 내야 질문에 이미 답변했는지 확인할 수 있습니다. 또한 파이썬 문서를 읽는 법을 배우기 원합니다. 그래서 예외, 오류, 나누기, 루프, 사례, 클래스, 디버깅 등을 읽을 수 있습니다. ... 다시 묻기 전에 – Pythonidaer

관련 문제