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"
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
在Python中,while循环应该允许您完成您的目标。你可以用这样的例子来解决你的问题:
如果我正确地理解了你的问题,这样的方法可能会奏效。
相关问题 更多 >
编程相关推荐