Python Code Problem

Chat about just about anything else

Python Code Problem

Postby Acid_1 on Tue Apr 08, 2008 5:14 pm

While this started as a school assignment, it is now a personal one. I can't get the program to loop. I just get an error. If someone would look over my source, that'd be cool.
Code: Select all
# calculates are for rectangles and circles them when provided

while True:
    print "welcome to the area calculation program"
    print "--------------------------------------"
    print

    # print out out the menu:

    # start
   
    print "please select a shape:"
    print "1 rectangle"
    print "2 circle"

    # user choice
    shape = input(">")

    # calculate area
    if shape ==1:
        height = input("please enter the height")
        width = input("please enter the width")
        area = height*width
        print "the area is", area
    else:
        pi = input("define pi")
        radius = input("please enter the radius:")
        area = pi*(radius**2)
        print "the area is", area
    print "again?"
    finished = input("y/n")
    if finished(y): break


It'd be much appreciated if someone could help me :P
User avatar
Acid_1
Level 5
Level 5
 
Posts: 817
Joined: Thu Nov 01, 2007 11:12 pm
Location: Saskatchewan, Canada

Linux Mint is funded by ads and donations.
 

Re: Python Code Problem

Postby ed@Mint on Tue Apr 08, 2008 6:39 pm

Hi Acid_1,

First, i'd like to say that Linux Mint forums are not the appropriate place to post about programming issues.
I suggest you to visit these forums : http://python-forum.org/pythonforum/index.php
In this case, since your problem is really simple, i'll help you :)

Second, give the error message as well, instead of saying "i have an error"

Those were the errors :
1) prefer using the raw_input() function than input() in those cases. It works the same and always returns a string.
2) you forgot the quotes ' ' around the characters when you test it :|
3) you inverted the condition to end the program

I'll even post the corrected code, but please do take in advice what i said above.

Code: Select all
# calculates are for rectangles and circles them when provided

while True:
   print "welcome to the area calculation program"
   print "--------------------------------------"

   # print out out the menu:

   # start

   print "please select a shape:"
   print "1 rectangle"
   print "2 circle"

   # user choice
   shape = raw_input(">")

   # calculate area
   if shape == '1':
      height = input("please enter the height")
      width = input("please enter the width")
      area = height*width
      print "the area is", area
   else:
      pi = input("define pi")
      radius = input("please enter the radius:")
      area = pi*(radius**2)
      print "the area is", area
   print "again?"
   finished = raw_input("y/n")
   if finished == 'n': break


Hope you'll find what you were looking for with this...
cheers
ed
In libro veritas

Now switched to : ArchLinux

New users, read the Linux Mint User Guide | Problem ? See the Wiki first | Check out the Software Portal | Join the IRC channel : #linuxmint @ irc.spotchat.org
User avatar
ed@Mint
Level 5
Level 5
 
Posts: 565
Joined: Sun Mar 09, 2008 6:03 pm
Location: Liege, Belgium

Re: Python Code Problem

Postby Acid_1 on Wed Apr 09, 2008 2:28 am

thanks man. I thought it was just open talk. Sorry. Anyhow thanks, and I'll remember you advice :P
User avatar
Acid_1
Level 5
Level 5
 
Posts: 817
Joined: Thu Nov 01, 2007 11:12 pm
Location: Saskatchewan, Canada


Return to Open chat

Who is online

Users browsing this forum: keithp and 2 guests