-1

I'm very new to programming and I'm sorry if this is an easy question. I want the code to replay itself after specific user inputs without having to manually run the code every time.

I've looked through similar questions on here and I see that others have had similar queries but I don't understand the answers given. I've tried the def main() at the beginning and put main() where I want the code to reset, but then the prompt for user input just doesn't show up.

def main():
    number = int(input("Please enter an integer between 0 and 127: "))
    if number > 127 or number < 0:
        print("I'm sorry, that is not an acceptable value. Please try again")
        main()
    elif number <= 127 and number >= 0:
        print("WIP")
    else:
        print("I'm sorry, something went wrong. Please try again and be sure to enter an integer between 0 and 127.")
        main()
Eric Reed
  • 377
  • 1
  • 6
  • 21
  • 1
    you can improve your code by adding a try catch block to raise an exeption when the input is not an integer try: number=int(input("Please enter an integer between 0 and 127: ")) except Exception as e: print("try to print an integer") – Ghassen Jun 26 '19 at 23:17
  • Note that there is no need for three "cases". after checking `number>127 or number<0`, the only other option is the right one – Tomerikoo Jun 26 '19 at 23:20
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Tomerikoo Jun 03 '21 at 17:02

2 Answers2

4

You're close. You need to call main in your python file to actually execute it the first time when your script is run

def main():
    number=int(input("Please enter an integer between 0 and 127: "))
    if number>127 or number<0 :
        print("I'm sorry, that is not an acceptable value. Please try again")
        main()
    elif number<=127 and number>=0 :
        print("WIP")
    else:
        print("I'm sorry, something went wrong. Please try again and be sure to enter an integer between 0 and 127.")
        main()

main()
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
-2

Welcome to coding! That's a neat attempt at trying to solve the problem. To get it to work, you need to make sure to call your main function at the bottom of your code (functions will not run unless you tell them to!

def main():
    number=int(input("Please enter an integer between 0 and 127: "))
    if number>127 or number<0 :
        print("I'm sorry, that is not an acceptable value. Please try again")
        main()
    elif number<=127 and number>=0 :
        print("WIP")
    else:
        print("I'm sorry, something went wrong. Please try again and be sure to enter an integer between 0 and 127.")
        main()

main()

While this will work, it might cause issues down the road. I would suggest using a while loop for this:

while True:
    number=int(input("Please enter an integer between 0 and 127: "))
    if number>127 or number<0 :
        print("I'm sorry, that is not an acceptable value. Please try again")
    elif number<=127 and number>=0 :
        print("WIP")
        break;
    else:
        print("I'm sorry, something went wrong. Please try again and be sure to enter an integer between 0 and 127.")

The while loop will run as the condition you give it is true. You can break out of the loop early by... well, using the break keyword.

Oreganop
  • 81
  • 1
  • I made this comment within two minutes of them. Simple deduction can probably tell you that I was typing this up at the same time as they were. It's not like those were hard recommendations to make. – Oreganop Jun 27 '19 at 20:38