number = get_int(input("Number: "))
passes a str object (which is what is returned from the input()
function) to our get_int()
function. Then, try: n = int(n)
, tries to type convert the srt object (which in n
in this function) to an int object.def get_int(n): try: print(f"Type converting {type(n)}") n = int(n) print(f"Type conversion to {type(n)} done.") return n except ValueError: print(f"Type conversion of {n} failed.") return False while True: number = get_int(input("Number: ")) if number: # do whatever print(number, type(number)) else: print("Error in number input")With a input of 12:
Output:Type converting <class 'str'>
Type conversion to <class 'int'> done.
12 <class 'int'>
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein