2017-02-07 4 views
-4

Это только код INPUT/OUTPUT Что такое: вы вводите «Область» или «периметр» или «том», и он запрашивает определенные строки и вычисляет их. Этот код проверяет длину и, если вход содержит цифры, и если он превышает длину, он выведет ошибку.Проверьте, содержит ли вход Python определенное слово

ЦЕЛЬ: Если «Зона», «Периметр» или «объем» находится на входе, перейти к следующей функции ..

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

у вас есть несколько вопросов. Проверьте ваши кавычки '' ', и если я введу« blah »прямо сейчас, ваш код подумает, что это« область »... почему бы не проверить' if find.lower() == 'area': ' – depperm

+6

Похоже, вы просто свалили ваш вопрос о домашнем задании. Мы вовсе не против того, чтобы помогать в выполнении домашних заданий, но он * должен быть [mcve]. На самом деле вы не сказали нам, в чем состоит ваша проблема. И как отметил деперм, у вас много. На самом деле большинство проблем. Это почти совсем неправильный способ выполнить то, что вы хотите. –

ответ

5

Вы просто можно сравнить строку вместо того, чтобы проверить его с длиной.
например:

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

и так .. по периметру и объема.

+0

вы хотите, чтобы это было 'find.lower()' на всякий случай – depperm

Смежные вопросы