2017-02-07 6 views
-4

이것은 입력/출력 코딩입니다. 무엇입니까? "영역", "둘레"또는 "볼륨"을 입력하면 특정 문자열을 묻고 계산합니다. 이 코드는 길이를 확인하고 입력에 숫자가 포함되어 있는지, 길이가 초과하면 오류를 인쇄합니다.파이썬 입력에 특정 단어가 포함되어 있는지 확인하십시오.

print ("This program will find the area, perimeter and volume of the Rectangle!") 

    find = input ("Type in one to calculate "area", "perimeter" and volume": ") 

    if len(find) == 4 and find.isalpha(): #This is area, people can type 4 chars and can get away with it, is there a way to fix it? 
     w = int (input ("What is the Width of the rectangle: ")) 
     l = int (input ("What is the Length of the rectangle: ")) 
     a = w*l 
     ans = "The area of the rectangle is %s units!" 
     print (ans%(a)) 
    elif len (find) == 6 and find.isalpha(): # This is Volume 
     w = int(input ("What is the Width of the rectangle: ")) 
     l = int(input ("What is the Length of the rectangle: ")) 
     h = int(input ("What is the Height of the rectangle: ")) 
     v = l*w*h 
     ans = "The volume of the rectangle is %s units!" 
     print (ans%(v)) 
    elif len (find) == 9 and find.isalpha(): #This is Perimeter 
     w = int (input ("What is the Width of the rectangle: ")) 
     l = int (input ("What is the Length of the rectangle: ")) 
     p = 2*(l+w) 
     ans = "The primeter of the rectangle is %s units!" 
     print (ans%(p)) 
    else: 
     print ("You spelled area, perimeter or volume wrong, or what you typed in includes NUMBERS!") 
+3

몇 가지 문제가 있습니다. 당신의 인용 부호를 확인하십시오'''그리고 만약 내가 지금 'ㅋㅋ'라고 입력하면 코드는 '영역'이라고 생각합니다 ... 만약 find.lower() == 'area':' – depperm

+6

만약' 숙제에 대한 질문 우리는 숙제를 돕는 것에 전혀 반대하지 않지만, 당신은 실제로 당신의 문제가 무엇인지를 말하지 않았습니다. 그리고 여기에서 지적 된 바와 같이, 실제로 대부분의 문제가 있습니다. 이것은 여러분이 원하는 것을 성취하기위한 거의 완전히 잘못된 방법입니다. –

답변

5

당신은 그것을 확인하는 대신 문자열을 비교 단지 수 있습니다 "지역이", "경계"또는 "볼륨"입력에있는 경우, 다음 기능으로 이동 .. : 목적이

길이.
예 : 주변 및 볼륨에 대한

if find == "area": #to check if the user input is "area" 

등등 ....

+0

당신은 그것을 위해서'find.lower()'가되기를 원할 것입니다. – depperm

관련 문제