1

I have written a code and it works, now as there are variable in the code I want at the end to ask the person if they wish to quit/continue, they say continue it goes all the way back to the first question. Also is there a way to ask at the start how many times the question is to repeat itself. Sorry can't upload code as it is over 150 lines long ta Greggy D

Greggy D
  • 121
  • 1
  • 3
  • 9
  • 5
    You can upload a smaller example / relevant parts, doesn't have to be the actual code but people would rather help with what you have tried and not code the whole solution for you. – jamylak May 12 '12 at 12:53

4 Answers4

2
i = 0

def code_to_repeat():
    # whatever is your code
    print "I was repeated : " + str(i) +" times"

while(True):
    print "Do you want to run the code (Y/N) : "
    stri = raw_input()
    print "\n"
    if stri=="Y":
        i += 1
        code_to_repeat()
    elif stri=="N"
        print "exiting\n"
        break;
    else:
        print "Please Answer Y/N only.\n"
sarveshseri
  • 13,738
  • 28
  • 47
  • I find this a little weird because it asks you if you want to run the code the first time through the loop... – mgilson May 12 '12 at 13:21
0

If I understand your question correctly, something like this might work.

def dostuff():
    ABC = raw_input("Enter Number (q exits): ")

    if(ABC.lower() == 'q'):  #Allow the user to enter q at this point to exit
       return False

    Product = int(raw_input("Enter Product:"))

    #do stuff to print out the cost of these items.

    #We could forgo the next lines and always return True here assuming the user
    #has more input if they didn't input 'q' for 'ABC'.  That's up to you.

    #return True

    print "Do you have more purchases [Y/N]?"
    answer=raw_input()
    return answer.upper() == 'Y'

while dostuff():
    pass

#same as:  
#while True:
#   if(not dostuff()): 
#      break
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Dude your code is a bit messy, you could just write for example `return answer.upper() == 'Y'` – Jakob Bowyer May 12 '12 at 13:05
  • @JakobBowyer : good point. didn't get a lot of sleep last night. Of course, `dostuff` might be better put into the main `while` loop as well. This is a little weird because even if the user wants to continue, they can't after a certain number of tries. But the basic idea is to use a `while` loop -- and I hope that was clear enough. – mgilson May 12 '12 at 13:08
  • OK here is relevent code ABC= raw_input("Enter Number: ") Product = int(raw_input("Enter Product:")) Once they enter the Number and then the product a $ amount for these items will appear. If they ahve more than 1 item to order it needs to go back to the first question, alternatively I could ask how many different orders are they to make. I tried your option mgilson but it did not work or not sure where to put it – Greggy D May 12 '12 at 13:09
  • @GreggyD -- I've edited a little. The idea here is basically the same as before... – mgilson May 12 '12 at 13:19
  • mgilson, I tried the code and it errors with a indent. I also copied your code into a seperate screen and it comes back with a syntax error. – Greggy D May 12 '12 at 13:31
  • @GreggyD -- sorry, forgot the ':' after my `if(ABC.lower() == 'q')`. Also, `pass` had 1 too few spaces before it. – mgilson May 12 '12 at 13:32
  • It looks like I have a indent error occuring, how do I check this. I have viewed other sites and they say -TT option or view whitespace, how is this done in python 2.6 – Greggy D May 12 '12 at 14:27
  • By the way you can do `return raw_input("> ").lower() == 'y'` ;D – Jakob Bowyer May 12 '12 at 16:48
  • @JakobBowyer -- Sure you can. For the purpose of writing an answer though, I like to have it explicit that `answer` is what the user is inputting, and it's a string and then testing if it is yes. Sometimes the extra line of code helps make it more clear what is happening to someone who may be new to the language. – mgilson May 12 '12 at 17:54
0

In Python, a while loop should allow you to accomplish your goal. You may use an example like this to solve your problem:

while(raw_input()[0] != 'n'):
    print 'to exit print n'
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Dmitry Zagorulkin
  • 8,370
  • 4
  • 37
  • 60
  • This works too, but you have no handle on what the user input -- So to keep going they just mash the keyboard and hit enter. It also fails if the user just hits "enter/return" without putting in any data. – mgilson May 12 '12 at 13:29
0

A while loop should work in your case.

while(raw_input("to exit enter n ")[0] != 'n'):
        print("Doing some work in the loop, until user enters an 'n'.")

raw_input()

is a good way to ask for user's input, and allows you to insert a prompt, like

to exit enter n.

Please bear in mind, you should check for more than 'n', like if the user pressed carriage return. Also, it might make sense to perform a simple parsing of the data, so you can do something more than just respond to whether or not someone entered n.

octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131