Jump to content

Need some help :) calculates the area and the perimeter

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
MisterSins

MisterSins

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
I'm learning Python and I have come across a little problem. So I'm coding a program that calculates the area and the perimeter, so here is some piece of the code:

welcome_mess = "\n Hello %s, "
name=raw_input('Insert your name ')
print welcome_mess % name
ar_pe=raw_input('Do you pretend to calculate the area or the perimeter? ')
objecto=raw_input('Rectangle or Square? ')
if (ar_pe =="Area") and (objecto=="Rectangle"):
    comp_ar_rect = input("\n Insert the lenght of the rectangle ")
    larg_ar_rect = input("\n Insert the width of the rectangle ")
    tot_area_rect = comp_ar_rect * larg_ar_rect
    print "\n The area is: " + `area`

elif...

This is only a piece of the code, so my problem is that I need the user to be able to calculate the area of a rectangle if ar_pe = area and objecto = rectangle, so if both of them are true the user can insert the values and calculate.

But after the user inputs area and rectangle the program closes and it doesn't ask the user for the values to calculate...

I think the problem is in the if statement, I googled this but I found nothing... :c-blink:

#2
Christopher87

Christopher87

    Newbie

  • Members
  • PipPip
  • 20 posts
Have you tried setting the input to lower-case before reaching your if statement?

#3
MisterSins

MisterSins

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Done I solved it :)

why lower-case?

#4
Christopher87

Christopher87

    Newbie

  • Members
  • PipPip
  • 20 posts
The reason being that AREA, area and Area would all be considered different inputs.

#5
MisterSins

MisterSins

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Yep I just saw that when I was testing it, I have to make it work with both...

#6
Christopher87

Christopher87

    Newbie

  • Members
  • PipPip
  • 20 posts
Do you know how to do that? :¬)

#7
MisterSins

MisterSins

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Well I was thinking about something like:

if ar_pe =="area" or "Area" or "AREA": 
but I bet there is a more simple way xD

#8
Christopher87

Christopher87

    Newbie

  • Members
  • PipPip
  • 20 posts
Yeah, there is :¬). Want me to enlighten you?

#9
MisterSins

MisterSins

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Sure it would be very helpful :P

#10
Christopher87

Christopher87

    Newbie

  • Members
  • PipPip
  • 20 posts
Okay. :¬)

#11
Christopher87

Christopher87

    Newbie

  • Members
  • PipPip
  • 20 posts
import string


name = raw_input("Name:")

name = string.lower(name)

print name


Very quick example :¬)

#12
MisterSins

MisterSins

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Thank you so much :D