![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Newbie
|
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:
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. |
|
|
|
|
|
|
#2 |
|
PFO Founder
![]() ![]() |
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. |
|
|
|
|
|
#3 |
|
Newbie
|
sorry but i do not understand how i would implement that while loop into my program? could give some example code?
|
|
|
|
|
|
#4 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
x = 0
while x < 5:
x = x + 1When 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("> ")![]() |
|
|
|
|
|
|
#5 | |
|
Newbie
|
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:
|
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
I can't see any while loop in that code you've posted. Also post the exact error message that you get
|
|
|
|
|
|
#7 | |||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
For instance: >>> input() sum([1, 2, 3]) 6 >>> raw_input() sum([1, 2, 3]) 'sum([1, 2, 3])' Some extra code is needed. The int function will turn a string into an integer (a whole number): >>> int(raw_input()) 10 10 >>> int(raw_input()) fish Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: invalid literal for int(): fish try:
number = int(raw_input("> "))
except ValueError:
print "That's not a number!"Quote:
Quote:
|
|||
|
|
|
|
|
#8 |
|
Newbie
|
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. |
|
|
|
|
|
#9 | ||||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
answer = 0
while answer != 2:
print """Choose an option:
1. Continue the loop.
2. Exit the loop."""
answer = input("> ")Quote:
Simply put, this is indented code: print "Starting test"
x = 1
if x == 1:
print "x == 1"
print "Ending test"print "Starting test" x = 1 if x == 1: print "x == 1" print "Ending test" Without indentation, Python has no way of telling what lines belong to the if statement. Quote:
Quote:
|
||||
|
|
|
|
|
#10 |
|
Newbie
|
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? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|