I have created a program in which the function will gets an user input as string and verifies with the another string using if condition, if satisfies we need to get the same output.
Program:
input_value_1 = input("Enter the first value = ")
input_type_assigned_1 = type(input_value_1)
print(input_type_assigned_1)
input_type = "<class 'str'>"
print(input_type)
def input_value():
if input_type == input_type_assigned_1:
return input_value_1
else:
"The input value is not valid"
input_value()
print(input_value())
Input Value = Ten
Ouput Value:
Enter the first value = Ten
<class ‘str’>
<class ‘str’>
None
Here we need to get an output as “Ten” instead it is showing as “None”
Can any one help me on this?
Problem statement = Giving an input value as “sting” and while comparing the user defined string variable with the incoming input data of “int”, “float” and “string”, the expected output should be the input data if the input data type is “string”.
Below is the changes made in code:
input_value_1 = input("Enter the first value = ")
input_type_assigned_1 = type(input_value_1)
print(input_type_assigned)
input_type = "<class 'str'>"
print(input_type)
def input_value():
if input_type_assigned_1 == input_type:
return input_value_1
else:
return "The input value is not valid"
input_value()
print(input_value())
Doubt:
Can we assign the object type (i.e. <class ‘str’>) to a variable (i.e. input_value() = type(“a”))?
The return type of input is always str. What is your definition of a “string”? Is “45” a string? Is “45abc” a string?
Yes. However, note that the object type of "a" is str, not "<class 'str'>". "<class 'str'>" is the representation of str, i.e. what gets output when you print it.
Requirement:
I will be giving an input value as a string to a variable, it needs to check whether the assigned input to a variable is a string, if the condition is true need to return the same value which is given as input, if not need to throw a message as “The input value is not valid”.
You are giving a string to a variable and and then ‘it needs’ to check whether variable is a string?!?? How come it can be anything than string if you providing a string?
If there is no correct problem description there can’t be correct solution.
All the inputs which we are assigning to a variable as “String”, while comparing it to a variable which is assigned with a value “<class ‘str’>”, if yes it need to print the input value.
Even though we give number the input value is taken as “String” right?
At that time while comparing the statement will be equal right?
input_value_1 = input("Enter the first value = ")
input_type_assigned_1 = type(input_value_1)
print(input_type_assigned_1)
input_type = "<class 'str'>"
print(input_type)
def input_value():
# Here we are comparing the input value "type" (i.e <class 'str'>) to the already defined variable with
# type value.
if input_type_assigned_1 == input_type:
return input_value_1
else:
return "The input value is not valid"
input_value()
print(input_value())
While giving value:
Enter the first value = 1 (Here the value is assigned to a variable as “String”)
So the if condition above will get true
Then it should give the same value as output right?
But while visualizing the code using the URL “Python Tutor code visualizer: Visualize code in Python, JavaScript, C, C++, and Java”
I came to know that the value of type is not found in the variable, thats y the code is not running properly.