Created
November 2, 2019 14:14
-
-
Save sojohnnysaid/9dbee060a630b2a39468e49416839a19 to your computer and use it in GitHub Desktop.
Python validating input getting started
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # get a positive integer from the user | |
| from cs50 import get_int | |
| def main(): | |
| x = getPositiveInteger("Please enter a positive integer: ") | |
| print(f"Thanks for entering {x}!") | |
| ''' | |
| for validating user input the logic is | |
| while true | |
| if this meets my criteria | |
| break | |
| ''' | |
| def getPositiveInteger(prompt): | |
| while True: | |
| n = get_int(prompt) | |
| if n >= 1: | |
| break; | |
| return n | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment