Python validating input getting started
This file contains 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