简体   繁体   中英

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

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.

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...

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 not in ["R", "C", "M"]:
      # Perhaps show an error message and not continue
      continue

     elif ps == "R":
       # function of r
     elif ps == "C":
     # ...

     # End of loop
     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  # break

I added 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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM