By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,996 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,996 IT Pros & Developers. It's quick & easy.

how can you loop a code

werecatz
P: 2
I'm trying to do a time/ distance calculator (like in the wiki guide)

Instead of just ending I want to be able to press or type something in and start from the begining (do a new calculation)

Expand|Select|Wrap|Line Numbers
  1. print "enter your distance/rate problem"
  2. rate= input("Rate: ")
  3. distance= input ("distance: ")
  4. print "Time:", distance/rate
that's the code I'm using.
May 2 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 511
I'm trying to do a time/ distance calculator (like in the wiki guide)

Instead of just ending I want to be able to press or type something in and start from the begining (do a new calculation)

Expand|Select|Wrap|Line Numbers
  1. print "enter your distance/rate problem"
  2. rate= input("Rate: ")
  3. distance= input ("distance: ")
  4. print "Time:", distance/rate
that's the code I'm using.
a loop ?
Expand|Select|Wrap|Line Numbers
  1. while 1:
  2.     print "enter your distance....
  3.     ...
  4.     print "Time: ...
  5.     choice = raw_input("do you want to continue: [y/n]" )
  6.     if choice in ["N","n"]:
  7.        break
  8.  
May 2 '07 #2

werecatz
P: 2
a loop ?
Expand|Select|Wrap|Line Numbers
  1. while 1:
  2.     print "enter your distance....
  3.     ...
  4.     print "Time: ...
  5.     choice = raw_input("do you want to continue: [y/n]" )
  6.     if choice in ["N","n"]:
  7.        break
  8.  
tried it didn't work (probably didn't put it in right)
A loop as in it goes back to the start so you can do another problem.
May 2 '07 #3

Expert 100+
P: 511
tried it didn't work (probably didn't put it in right)
A loop as in it goes back to the start so you can do another problem.
if it doesn't work, then show your error messages or the output you have
for me it works
Expand|Select|Wrap|Line Numbers
  1. # ./test.py
  2. enter your distance/rate problem
  3. Rate: 10
  4. distance: 10
  5. Time: 1
  6. do you want to continue: [y/n]y
  7. enter your distance/rate problem
  8. Rate:
  9.  
Expand|Select|Wrap|Line Numbers
  1. # ./test.py
  2. enter your distance/rate problem
  3. Rate: 10
  4. distance: 10
  5. Time: 1
  6. do you want to continue: [y/n]n
  7. #:
  8.  
May 2 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.