I wanted to ask,
Is there a way that you can re - run any sort of program on Python 3.x or 2.x without the need for a while loop?
For instance, is there any block of code that I can use to do this:
if user says yes:
re - run code from top
else:
end program
For instance, take this example:
output Hello what would you like to do?
if user says internet
response = go to internet.
if response = true
go to start.
else
end program
def internet
open web page.
output Would you like to do anything else?
if input is yes
return True
else
return False
Thanks, Judy.
Note, the code above is a mock - up.
This works...
>>> from itertools import count
>>> c = count()
>>> for i in c:
... i = input("what is your input? ")
... if str(i) == "internet":
... print "internet"
... elif i == True:
... continue
... else:
... break
...
what is your input? True
what is your input? "internet"
internet
what is your input? "foo"
>>>
But it's still much better as a while
loop.
The while
is actually the best and cleanest solution here. Remember to place all re-usable functionality in functions or classes. Eg:
def main():
while True:
fun()
ask user
if user says no:
break
def fun():
# your functionality goes here
if __name__ == "__main__":
main()
while user says no:
if user says yes:
do stuff
break
This gets rid of one of the statements.
You can restart the execution of a script with os.exec*()
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.