• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Prompting users for input, then verifying what they've inputted
#1
Here's how to make two simple scripts that'll check if the user has inputted correctly.

Input a number

Prompting the user to input a number, if they don't enter a number then it'll re-prompt them.

[code=python]
def number():
input_number = raw_input("Please input a number: ")
if not input_number.isdigit():
print "Error: That is not a number. Input a number"
number()
if input_number.isdigit():
print "That is a number! Thank you. \n"
raw_input("Press enter to exit..")
number()
[/code]


Input a word

Prompting the user to enter a word, if they don't enter a single word it re-prompts.

This is essentially the same as the above script, except instead of "isdigit" we'll be using "isalpha".

[code=python]
def start():
letter_input = raw_input("Please input a letter: ")
if not letter_input.isalpha():
print "Error: That is not a letter. Please input a letter"
start()
if letter_input.isalpha():
print "That is a letter! Thank you. \n"
raw_input("Press enter to exit..")
start()
[/code]


Simple yet effective. :yep
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Prompting for an existing path and file name, if invalid, reprompt. Mark 0 5,776 13-07-2010, 01:49 PM
Last Post: Mark

Forum Jump: