So I was making a console application using Python 3.7; that heavily depends on input (wowz).
The application's function is to "swap" between two integer variables' values. And that is not where the problem is at, the problem is when I try to validate the user's input by checking the data-type for the input using a couple "if statements", and no matter what the user inputs using the "input()" function; the input's data-type will always be defined as ""
I just want this little piece of ART to run dynamically. (I want the input() function to dynamically detect the data-type of the input and assign it to the variable? plsss)
P.S.: I didn't try anything, since all I found was useless; I guess. (tells me to use the int() function, e.g.: {
# Works just fine when input is fully composed of integral numbers.
a = int(input("Enter an integer as a value for A: "))
} (EDIT OF Line 5 BELOW)
I don't want to use the int() function; since it'll cause a pile of problems, if the user's input was a string that was not fully composed of integral numbers. (ValueError)
def swap(x, y):
return y, x
aValid = 1
bValid = 1
a = input("Enter an integer as a value for A: ")
if (str(type(a)) != "<class 'int'>"):
print("Please take us seriously, we're watching you.\n")
aValid = 0
while (aValid == 0):
a = input("Enter an integer as a value for A: ")
if str(type(a)) != "<class 'int'>":
print("Please take us seriously, we're watching you.\n")
aValid = 0
else:
aValid = 1
# ! To be worked on:
b = input("Now, Please enter the value for B: ")
print("A = " , a)
print ("B = ", b)
a, b = swap(a, b)
print("Now:\nA = ", a)
print("B = ", b)
I expected the input() function in Python 3.7 (32bit) to dynamically detect the data-type of the input and assign it to the variable along with the input itself.
But what actually happens is that it always assigns the input's data-type as "< class 'str' >"; (no spaces after < and before >, ) which causes the program to go into an infinite loop, and it is giving me a headache; my stupidity.
So, in python 2 the input() function would detect the type of the user's input. However, in python3 input() refers to raw_input() in python 2. In order to dynamically detect the type, you can use the ast, specifically ast.literal_eval.
You could potentially write your own input function as follows:
import ast
def input_detect(s):
return ast.literal_eval(input(s))
and then your code would look like:
import ast
def input_detect(s):
return ast.literal_eval(input(s))
def swap(x, y):
return y, x
aValid = 1
bValid = 1
a = input_detect("Enter an integer as a value for A: ")
if (str(type(a)) != "<class 'int'>"):
print("Please take us seriously, we're watching you.\n")
aValid = 0
while (aValid == 0):
a = input_detect("Enter an integer as a value for A: ")
if str(type(a)) != "<class 'int'>":
print("Please take us seriously, we're watching you.\n")
aValid = 0
else:
aValid = 1
# ! To be worked on:
b = input_detect("Now, Please enter the value for B: ")
print("A = " , a)
print ("B = ", b)
a, b = swap(a, b)
print("Now:\nA = ", a)
print("B = ", b)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With