How do i make code restart/repeat?

How do i make it so that code repeats or restarts from the beginning?

@Speep If you would like code to infinitely repeat, over and over, try this:

while True:
  # Put any code to repeat infinitely here

Otherwise, I’m not 100% sure what you mean. If this doesn’t answer your question, could you elaborate?

3 Likes

ye mb i meant something that allows the code to run but after its done, repeats it instead of running the code in the next lines.

@Speep Could you give me a example of a proposed use case?

lets say i want a book to read and i asked by the code what type of book i want, i find a book, but then i want the code to ask what type of book i want again. (man i shouldve stayed on scratch huh?)

@Speep To do that, the code would be as follows:

while True:
  book = input("What book do you want to read?")
  print(book + " is a very good book!"

This will ask the user what book they want to read and print book + " is a very good book" infinitely and it will never stop.

Alr fine ima show you ma code (im new so idk what if im doing is correct)(the … is placeholder)

ServerType = input ("What type of MC server do you want to see? ")
if ServerType == "Survival":
 print ("=== Survival Servers ===")
 print ("AsianF4rmer, ArchMC, Legend SMP")
 SurvLink = input ("Which Server do you want the link to? (type name as shown) ") 
 if SurvLink == "AsianF4rmer": ...
 if SurvLink == "ArchMC": ...
 if SurvLink == "LegendSMP":  ...
kill = input ("Is that all? (answer Y or N) ")
if kill == "Y":
 quit()
else:
 if ServerType == "PvP":
  print ("=== PvP Servers ===")
  print ("Zentic, MercuryMC")
PvPLink = input ("What Server do you want the link to? ")
if PvPLink == "Zentic": ...
if PvPLink == "MercuryMC": ...

how do you make it start from beginning after it ends/quits?

@Speep If you’d like that to loop infinitely, just wrap it in a while True:, like this:

while True:
  ServerType = input ("What type of MC server do you want to see? ")
  if ServerType == "Survival":
   print ("=== Survival Servers ===")
   print ("AsianF4rmer, ArchMC, Legend SMP")
   SurvLink = input ("Which Server do you want the link to? (type name as shown) ") 
   if SurvLink == "AsianF4rmer": ...
   if SurvLink == "ArchMC": ...
   if SurvLink == "LegendSMP":  ...
  kill = input ("Is that all? (answer Y or N) ")
  if kill == "Y":
   break()
  else:
   if ServerType == "PvP":
    print ("=== PvP Servers ===")
    print ("Zentic, MercuryMC")
  PvPLink = input ("What Server do you want the link to? ")
  if PvPLink == "Zentic": ...
  if PvPLink == "MercuryMC": ...

Or, you can make it loop until a certain condition is not met, like this:

while not(var == "something"):
  ServerType = input ("What type of MC server do you want to see? ")
  if ServerType == "Survival":
   print ("=== Survival Servers ===")
   print ("AsianF4rmer, ArchMC, Legend SMP")
   SurvLink = input ("Which Server do you want the link to? (type name as shown) ") 
   if SurvLink == "AsianF4rmer": ...
   if SurvLink == "ArchMC": ...
   if SurvLink == "LegendSMP":  ...
  kill = input ("Is that all? (answer Y or N) ")
  if kill == "Y":
   break()
  else:
   if ServerType == "PvP":
    print ("=== PvP Servers ===")
    print ("Zentic, MercuryMC")
  PvPLink = input ("What Server do you want the link to? ")
  if PvPLink == "Zentic": ...
  if PvPLink == "MercuryMC": ...

while not doesnt work according to something called “ruffle”?
btw while true just repeats the first question regardless of the input :frowning:

This is not true. I use it quite often.


What specifically would you like to change?

i think that while not(var == "something"):will work but idk what to insert into var and something … (average scratcher moment)

@Speep What variable will define whether or not to repeat the code again?

why not just

while var != "something":
    ...

@QwertyQwerty88

That works too. Wasn’t thinking.

But what would i insert in var+something???
(brain fried rn) :sob:

@Speep var is the name of any variable (var is just a placeholder) and "something" is the value of the variable.

basically you don’t need var at all and just use while True

@Speep If you wanted to make your code repeat, while True is the code you need. If you want your code to repeat until a coindtion is not met, put something in place of “True”.

1 Like

ok thanks (brain still fried after all the waffle)

1 Like

Please mark the post that helped you most as the solution.