repl.it
@adammorris/

TeachingLoopsRecusionValidation

Python

No description

fork
loading
Files
  • main.py
main.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
"""
Teaching students different ways of implementing a way to "get_input". Different functions with same signature that returns the integer of the string the user input

Limitation: only gets single digit numbers. Later I would teach them the method "".isdigit to get strings of multiple digits
"""

def get_integer_while(prompt: str) -> int:
    condition: bool = True
    while condition:
        user_input: str = input(prompt)
        if user_input in "0123456789":
           condition = False
    return int(user_input)

def get_integer_while_2(prompt: str) -> int:
    while True:
        user_input: str = input(prompt)
        if user_input in "0123456789":
            break
    return int(user_input)

def get_integer_recursion(prompt: str) -> int:
    user_input: str = input(prompt)
    if not user_input in "0123456789":
        return get_integer_recursion(prompt)
    return int(user_input)

result: int = get_integer_while("Input one integer: ")
print(result)
result: int = get_integer_while_2("Input one integer: ")
print(result)
result: int = get_integer_recursion("Input one integer: ")
print(result)
?