2017-02-07 5 views
-4

Dies ist nur INPUT/OUTPUT-Codierung Was ist das: Sie geben "Area" oder "Perimeter" oder "Volume" und es fragt nach bestimmten Zeichenfolgen und berechnet es. Dieser Code prüft auf die Länge und wenn die Eingabe Zahlen enthält, und wenn sie über die Länge ist, wird der Fehler gedruckt.Prüfen, ob Python-Eingabe ein bestimmtes Wort enthält

ZIEL: Wenn „Area“, „Umfang“ oder „Lautstärke“ in der eingegeben wird, in die nächste Funktion gehen ..

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

Sie haben mehrere Probleme. Überprüfen Sie Ihre Anführungszeichen '' 'und wenn ich jetzt 'blah' eingebe, denkt Ihr Code, dass es 'Bereich' ist ... warum nicht check' if find.lower() == 'Bereich': ' – depperm

+6

Das hört sich so an, als hättest du gerade abgeladen Ihre Hausaufgabenfrage: Wir sind überhaupt nicht dagegen, Hausaufgaben zu machen, aber es muss ein [mcve] sein. Sie haben uns eigentlich nicht gesagt, was Ihr Problem ist. Und wie depeperm darauf hingewiesen hat, Ich habe viele. Eigentlich die meisten Probleme. Dies ist fast völlig die falsche Art und Weise zu erreichen, was Sie wollen. –

Antwort

5

Sie können einfach nur vergleichen die Zeichenfolge, anstatt es zu prüfen mit Länge.
zB:

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

und so .. für Umfang und Volumen.

+0

Sie wollen es 'find.lower()' nur für den Fall sein – depperm

Verwandte Themen