-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcinput.py
41 lines (35 loc) · 1.16 KB
/
pcinput.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def get_float(prompt):
"""Prompts for a floating number and ensures the input is a float"""
while True:
try:
num = float(input(prompt))
except ValueError:
print("That is not a number -- please try again")
continue
return num
def get_integer(prompt):
"""Prompts for an integer and ensures the input is an integer"""
while True:
try:
num = int(input(prompt))
except ValueError:
print("That is not an integer -- please try again")
continue
return num
def get_string(prompt):
"""Prompts for a string of any kind, be it characters or numerals"""
line = input(prompt)
return line.strip()
def get_letter(prompt):
"""Prompts for a single character ensuring only a single one is entered"""
while True:
line = input(prompt)
line = line.strip()
line = line.upper()
if len(line) != 1:
print("Please enter exactly one character")
continue
if line < 'A' or line > 'Z':
print("Please enter a letter from the alphabet")
continue
return line