Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python validating input getting started
# 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