Home > other >  How to make this code go back the start of the problemstate?
How to make this code go back the start of the problemstate?

Time:05-17

I am trying to create a program where you can choose 3 options. Review, cheat sheet and math equations but don't worry about that. Is there any way to loop "def r()" to "problemstate()" to the start WHILE saving the stuff the user wrote in "inputr". Essentially i'm trying to type stuff in the inputr, save whatever the user wrote in there, then type a letter or something to go back to beginning. And repeating this process over and over again. No need to terminate. This is the link to the code please and thank you. https://trinket.io/python3/6e76fbb2f1 Code image incase you can't see the trinket.

CodePudding user response:

You're close, just a bit off on what def is doing. What you want is to define your 4 functions (problemstate(), r(), c(), and m()) first. Then have a loop that looks like this:

while (some_condition):
  ps = problemstate()
  if ps== 'R':
    r()

  # etc...

So have your problemstate() function return the input. Or even better, you don't need problemstate() at all:

while (some_condition):
  print("What would you like to use this program for?")
  print("(R)eviewing, (C)heat sheet, or (M)ath?")
  ps = input("Please select the letter appropriately? ")

  if ps == 'R':
    r()

  # etc...

CodePudding user response:

You only have to add a infinite loop, there are many different ways of do that (in my example I use while True) an use continue to repeat the process.

def problemstate():
   while True:
     # HERE: It is possible add a function to clean the console if you want
     print("What would you like to use this program for?")
     print("(R)eviewing, (C)heat sheet, or (M)ath?")
     ps = input("Please select the letter appropriately? ")
     
     if ps != "R" or ps != "C" or ps =! M:
      # Perhaps show an error message and not continue
      continue

     elif ps == "R":
       input_learning = input("Type all of the learning and knowledge you would like to use: ") 
       inputr = input("\nType 'p' if you want to refer back to the start and pick a different sheet or any other letter to exit: ")
       if inputr == 'p':
         continue
       else:
         raise SystemExit
     # ...

I think r() was unnecessary, because the infinite loop does the functionality. I add some conditions to review that the selected option, even you can show a message error when ps is invalid.

Also, I added a new variable to save the knowledge learned (you can write this in a file for example) and inputr just to enter what the user wants to do.

  • Related