Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 2nd, 2006, 11:38 AM   #1
Volkan
Newbie
 
Join Date: Feb 2006
Location: London-Uk
Posts: 7
Rep Power: 0 Volkan is on a distinguished road
Send a message via MSN to Volkan
Python area calculator

I have made a Python circle and rectangle area calculator BUT i want to make it loop back to asking for input after it has done a function so say
i chose rectangle as my shape entered my values and got my answer, after giving me the answer i want it to ask which shape to calculate again so that the user doesnt have to keep opening and closing the module.
Quote:
print "Welcome to the Area Calculator"
print "------------------------------"
print "Written By Volkan Kiziltug"
print
print "please select a shape:"
print "1 Rectangle"
print "2 Circle"

shape = input("> ")
if shape == 1:
height = input("Please enter the height: ")
width = input("please enter the width: ")
area = height*width
print "The area is", area

else:
radius = input("Please enter the radius: ")
area = 3.14*(radius**2)
print "The area is", area
i dont know how to make it ask to choose another shape again.
any help will be greatfully accepted i know you guys are pros here
i started python yesterday, been looking for some good tutorials etc. but i think im going to have to get a book.
Volkan is offline   Reply With Quote
Old Feb 2nd, 2006, 12:03 PM   #2
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,613
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
Well the way you could make it go back is do a while loop. Like while not q do loop

http://en.wikibooks.org/wiki/Program...ol#While_loops
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Feb 2nd, 2006, 12:13 PM   #3
Volkan
Newbie
 
Join Date: Feb 2006
Location: London-Uk
Posts: 7
Rep Power: 0 Volkan is on a distinguished road
Send a message via MSN to Volkan
sorry but i do not understand how i would implement that while loop into my program? could give some example code?
Volkan is offline   Reply With Quote
Old Feb 2nd, 2006, 1:18 PM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Quote:
Originally Posted by Volkan
sorry but i do not understand how i would implement that while loop into my program? could give some example code?
A while loop repeats until a condition fails. For instance:
x = 0
while x < 5:
    x = x + 1
In the above program x starts as zero. The program then keeps adding 1 to x, but only while x is less than five. This is why it's called a 'while' loop. Because it will keep repeating the same bit of code while x is less than five.

When x reaches five, the condition is broken, and the loop exits.

What you want to do is keep asking the user questions while he does not want to quit. Here's a simple loop example that controlled by user input:

answer = 0

while answer != 2
    print """Choose an option:
    1. Continue the loop.
    2. Exit the loop."""

    answer = input("> ")
I'm sure someone else will point out that raw_input is usually preferable to input. But I'll leave that for someone else to explain, because I have to go
Arevos is offline   Reply With Quote
Old Feb 2nd, 2006, 2:30 PM   #5
Volkan
Newbie
 
Join Date: Feb 2006
Location: London-Uk
Posts: 7
Rep Power: 0 Volkan is on a distinguished road
Send a message via MSN to Volkan
Ok i got it to start looping once, but ive discovered a problem which i had over-looked which is when i hit enter without entering a value (1 or 2) it just has an incorrect syntax error because nothing was defined.

the other problem i have is that i can only get it to loop once.

code is below.

Quote:
#Name:Area and Circumference Calculator
#Author:Volkan Kiziltug
#Date:02/02/06 Thursday 17:45
#Descriptionrogram to calculate the area of a rectangle or the circumference of a circle
#Comment:Works on all python version and systems

#begening

print "------------------------------"
print "Welcome to the Area Calculator"
print "------------------------------"
print
print "please select a shape:"
print "1 Rectangle"
print "2 Circle"

shape = input("> ")
if shape == 1:
height = input("Please enter the height: ")
width = input("please enter the width: ")
area = height*width
print "The area is", area

else:
radius = input("Please enter the radius: ")
area = 3.14*(radius**2)
print "The area is", area
answer = 0

#looped section below

def calculator():
print
print "------------------------------"
print "Welcome to the Area Calculator"
print "------------------------------"
print
print "please select a shape:"
print "1 Rectangle"
print "2 Circle"

shape = input("> ")
if shape == 1:
height = input("Please enter the height: ")
width = input("please enter the width: ")
area = height*width
print "The area is", area

else:
radius = input("Please enter the radius: ")
area = 3.14*(radius**2)
print "The area is", area
answer = 0


class _Exit:
def __repr__(self):
raise SystemExit

exit = _Exit()
exit # exits the interpreter

print ""
choice = raw_input("Calculate More? Y or N: ").upper()
if choice == 'Y':
calculator()

else:
_Exit()
print
print "Bye! "




_Exit()


print
print "-------------------------------------------"
print "This program was written by Volkan Kiziltug"
print "-------------------------------------------"

Volkan is offline   Reply With Quote
Old Feb 2nd, 2006, 3:00 PM   #6
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
I can't see any while loop in that code you've posted. Also post the exact error message that you get
Cerulean is offline   Reply With Quote
Old Feb 2nd, 2006, 3:38 PM   #7
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Quote:
Originally Posted by Volkan
Ok i got it to start looping once, but ive discovered a problem which i had over-looked which is when i hit enter without entering a value (1 or 2) it just has an incorrect syntax error because nothing was defined.
This is because you're using input, and not raw_input. The former takes in python code from the user. The latter takes in the raw string of exactly what the user typed.

For instance:
>>> input()
sum([1, 2, 3])
6
>>> raw_input()
sum([1, 2, 3])
'sum([1, 2, 3])'
So the first thing to do is to use raw_input. But that only gets strings. It doesn't convert the string the user typed, into a number Python can understand.

Some extra code is needed. The int function will turn a string into an integer (a whole number):
>>> int(raw_input())
10
10
So far so good. But what occurs when a user enters in a word?
>>> int(raw_input())
fish
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): fish
We get a value error. Which is fair enough; fish isn't usually considered to be a number. What we need to do now is tell Python what to do when it comes across this error. This requires the 'try' and 'except' keywords:
try:
    number = int(raw_input("> "))
except ValueError:
    print "That's not a number!"
The above code tries to get a number from a user. If it fails, it raises an exception - in this case, ValueError. When it receives a ValueError, usually the program will end; but we've overridden this behavior and made it print out "That's not a number!" instead of quitting.

Quote:
Originally Posted by Volkan
the other problem i have is that i can only get it to loop once.
As Cerulean says, there's no while loop in your code. You need a while loop somewhere in order to get it to repeat.

Quote:
Originally Posted by Volkan
code is below.
When you copy your code, you've somehow lost the indentation. Because Python makes use of indentation, it's very difficult to see what the problem is in your program without it.
Arevos is offline   Reply With Quote
Old Feb 2nd, 2006, 3:47 PM   #8
Volkan
Newbie
 
Join Date: Feb 2006
Location: London-Uk
Posts: 7
Rep Power: 0 Volkan is on a distinguished road
Send a message via MSN to Volkan
hmm i looped it not usign a conventional loop as that method using the WHILE tag didnt work, also i noticed no "indentation" which many tutorials and people talk about.
i get no indentation i used PYTHON IDLE GUI version to make this.

print ""
choice = raw_input("Calculate More? Y or N: ").upper()
if choice == 'Y':
calculator()

that is what i used to get it to go back and do more calculation.
Volkan is offline   Reply With Quote
Old Feb 2nd, 2006, 3:59 PM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Quote:
Originally Posted by Volkan
hmm i looped it not usign a conventional loop as that method using the WHILE tag didnt work,
That may have been a mistake on my part. Somehow I missed of the colon at the end of the condition. Try the following (fixed) code:
answer = 0

while answer != 2:
    print """Choose an option:
    1. Continue the loop.
    2. Exit the loop."""

    answer = input("> ")
Quote:
Originally Posted by Volkan
also i noticed no "indentation" which many tutorials and people talk about.
Indentation is a concept vital to Python. Much of Python's syntax hinges around it.

Simply put, this is indented code:
print "Starting test"
x = 1
if x == 1:
    print "x == 1"
print "Ending test"
And this is not:
print "Starting test"
x = 1
if x == 1:
print "x == 1"
print "Ending test"
The indented line in the first example (print "x == 1"), tells Python that this line of code 'belongs' to the if statement above it. The line of code will execute only if the condition is correct, if the value of x is 1.

Without indentation, Python has no way of telling what lines belong to the if statement.
Quote:
Originally Posted by Volkan
i get no indentation i used PYTHON IDLE GUI version to make this.
Likely Idle automatically indents things for you.

Quote:
Originally Posted by Volkan
print ""
choice = raw_input("Calculate More? Y or N: ").upper()
if choice == 'Y':
calculator()

that is what i used to get it to go back and do more calculation.
You're using a technique called recursion, as opposed to using a while loop, which is called iteration. The major disadvantage with recursion in Python is that it's finite. Python can only handle a certain number of recursions (although this is a large number) before it runs out of memory. With iteration, you can loop as many times as needed.
Arevos is offline   Reply With Quote
Old Feb 2nd, 2006, 5:31 PM   #10
Volkan
Newbie
 
Join Date: Feb 2006
Location: London-Uk
Posts: 7
Rep Power: 0 Volkan is on a distinguished road
Send a message via MSN to Volkan
ahh yeah yeah my indentation is ok.
i thought you meant indented as in like italic or something
i used the bit of code you gave which sued the whil command etc.
but i dont understand how to use it, i put it in, it asks to continue the loop i press 1 to continue and it just ends the script..
im guessing i need to define where it needs to loop to?
Volkan is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC