2016-06-05 3 views
1

Python 입문. 저는이 코드를 작성하고 있습니다. 수영과 싸이클을위한 입력이 'Y'와 'N'의 조합 일 때, 내 프로그램에서 else 문 (yourName, '연습이 필요함) . 내가 도대체 ​​뭘 잘못하고있는 겁니까? 수영 만약, 위의 예를 들어, 파이썬에서입력을 불리언 표현식과 비교하는 파이썬

if swim is 'Y' and cycling is 'Y' or swim is 'y' and cycling is 'y': 

:

def main(): 

    yourName = input("What is the your name? ") 
    swim = input("Can you swim <Y>es or <N>o? ") 
    cycling = input("Can you cycle <Y>es or <N>o? ") 

    if swim and cycling is 'Y' or swim and cycling is 'y': 
      print(yourName, 'is an athlete.') 
    elif swim and cycling is 'N' or swim and cycling is 'n': 
     print(yourName,'shows potential.') 
    else: 
     print(yourName,'needs practise') 

main() 
+0

예. 따라서 두 입력이 모두 'y'이면 사람은 운동 선수가됩니다. 둘 다 'n'이면 연습이 필요하지만 입력 중 하나가 'y'이고 다른 하나가 'n'이면 잠재적 진술이 표시되기를 원합니다. – Tom

답변

0

당신이이 방법을 수행 할 수 있습니다 char가 또는 'n'을 'Y'입니다

if swim.lower() == <char> <conditional operator> cycling.lower() == <char> :

.

def main(): 

    yourName = input("What is the your name? ") 
    swim = input("Can you swim <Y>es or <N>o? ") 
    cycling = input("Can you cycle <Y>es or <N>o? ") 

    is_swim = swim.lower() 
    is_cycle = cycling.lower() 

    if is_swim == 'y' and is_swim == 'y': 
      print(yourName, 'is an athlete.') 
    elif is_swim == 'y' or is_cycle == 'y': 
     print(yourName,'shows potential.') 
    else: 
     print(yourName,'needs practise') 

main() 

str.lower()은 문자열을 소문자로 변환합니다.

+0

더 큰 공간에서 설명해 주시겠습니까? 내 변수 등으로 어떻게 쓸 수 있습니까? – Tom

+0

@Tom, 코드에서 변경했습니다. – SilentMonk

+0

감사합니다. @SilentMonk. 많이 감사합니다 – Tom

1

당신은 같은 조건을 변경해야 은 수영이 경우 사실 인 존재하는지 여부를 의미합니다.

+1

입력 이후로 문자열이 if 입력되었습니다. 따라서 거짓 사례는 여기에 오지 않을 것입니다. 나도 그렇게 생각해. 하지만 고마워, 내가 문제를 특정 성명을 만드는 편집 amde. – rkatkam

0

여기에서는 원하는 논리를 완전히 분명히하지 않았지만 목표가 수영이나 사이클에 '예'인 경우 '잠재력을 보여줍니다'라고 표시하면 '운동 선수'와 '연습이 필요합니다' 아래 코드는보다 읽기 쉬운 옵션입니다.

def main(): 

    yourName = input("What is the your name? ") 
    swim = input("Can you swim <Y>es or <N>o? ") 
    cycling = input("Can you cycle <Y>es or <N>o? ") 

    swims = swim and swim.upper() == "Y" 
    cycles = cycling and cycling.upper() == "Y" 

    if swims and cycles: 
     print(yourName, 'is an athlete.') 
    elif (swims or cycles): 
     print(yourName,'shows potential.') 
    else: 
     print(yourName,'needs practise') 

main() 
+0

감사합니다. Jeremy가 정확히 내가 찾고있는 것입니다. 감사합니다 – Tom

+0

위대한 @ 톰,이 질문에 대한 답변을 수락하시기 바랍니다. –

관련 문제