Easily manage getting user inputs for all data types
Project description
EzInputs
EzInputs is built to get rid of the need to put while loops and try/except blocks around your type cast input() calls
The old way
while True:
num = input("Enter a number: ")
try:
num = int(num)
break
except ValueError:
print("Invalid integer, try again")
The EzInputs way
import ezinputs
num = ezinputs.int_input()
Depending on how you measure it, ezinputs is about 10 times better!
Docs & Useage
Before we begin, these functions are all in the ezinputs.py file that ships in this package. Therein, full documentation is given for each function.
Primitive types
In this section we will look at the 4 primitive input types ezinputs has to offer.
All basic input types have two optional arguments;
prompt
- the input prompt shown to the user (function-specific default)
error
- the message displayed when an invalid value is entered (function-specific default)
Integer input
>>> ezinputs.int_input()
Enter an integer: 5
(returns)
5
Float input
>>> ezinputs.float_input()
Enter a float: 12.23
(returns)
12.23
Boolean input
>>> ezinputs.bool_input()
Enter boolean: True
(returns)
True
String input
Please just use the builtin python input(), this str_input() function is extremely redundant!
>>> ezinputs.str_input()
Enter a string: hello world!
(returns)
'hello world!'
Collection types
In this section we will look at the 4 collection input types ezinputs has to offer
The collection inputs have additional optional arguments on top of the besic input arguments;
astype
- the datatype required for values (defaults to str)
count
- the number of items to retrieve (defaults to 1)
List input
>>> ezinputs.list_input(count=2, astype=int)
Enter item 1: 5
Enter item 2: 12
(returns)
[5, 12]
Set input
>>> ezinputs.set_input(count=3, astype=float)
Enter item 1: 7.6
Enter item 2: 2
Enter item 3: 4
(returns)
{2.0, 4.0, 7.6}
Tuple input
>>> ezinputs.tuple_input(count=3, astype=bool)
Enter item 1: a
Enter item 2: 1
Enter item 3: True
(returns)
(True, True, True)
Dictionary input
>>> ezinputs.dict_input(count=3)
Enter key 1: sugar
Enter str value for sugar: 150g
Enter key 2: flour
Enter str value for flour: 400g
Enter key 3: insert_cake_ingredient_here
Enter str value for insert_cake_ingredient_here: some
(returns)
{'sugar': '150g', 'flour': '400g', 'insert_cake_ingredient_here': 'some'}
Author
Will Johnson
Discord: __wcj__#3111
Email: 277willjohnson@gmail.com
Website: https://heyteach.co.uk/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.