1

I wrote this code but my instructor said that the program should loop for new input until told to stop by the person running the program.

*hint: an input of zero (0) kilometers could be used to stop the program.

Can someone show me how to include that in my code? I used x=raw_input(), since it closes so fast in cmd when I ran the program.

def kilo():
    kilometers = input("Please enter a distance in kilometers?:") 
    return kilometers

def miles(km):
   """calculates miles from kilometers km""" 
   miles = km * 0.6214
   return miles    

def main():
  print "This program converts kilometers to miles." 
  km = kilo()
  print miles(km) 

  main()

 x= raw_input()
1
  • @Yann: the homework tag is deprecated (see the link on the "Community Bulletin" notes on the right side of the page). Commented Sep 27, 2012 at 20:07

1 Answer 1

2

You don't need a main() routine.

Instead of def main():, you can use a while True: loop. And if km happens to be returned as 0, you could break out of the loop:

if km==0:
    break

Also, better be explicit and use kilometers = float(raw_input("Please enter...")).

Sign up to request clarification or add additional context in comments.

4 Comments

ok so if I dont declare to stop the program will run in the loop right?
Depends on what you mean by "declare", but generally, yes, that's the idea.
like my instructor expected my program to run ask user if they want to run the program again if yes "please enter yes" if not please enter no" so I was trying to figure out how to use that will my code
But your instructor also gave you a hint about how you could stop the program. You might want to add this in the raw_input() prompt, so the user knows what to do...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.