Need a solution for below code getting an output

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?

It’s not clear to me what you are trying to do. Under what condition should the input be returned as is, and when should it be considered invalid?

As is, your input_value function has some problems:

if input_type == input_type_assigned_1:

The input_type variable is "<class 'str'>", and input_type_assigned_1 is str, so this condition is always False.

else:
        "The input value is not valid"

This doesn’t do anything. You probably forgot to return.

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”))?

Thank you for your reply.

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.

I understood what you said.

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”.

Can you please share me the code for this?

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.

Screenshot:

Now you can understand what i am saying.
Can you please suggest me the correction need to be done on the code, or else please share me the code.

Actually no.

Nevertheless some lines of code:

>>> type('abc') == "<class 'str'>"
False
>>> type('abc').__name__ == 'str'
True
1 Like

The proper way to check if something is a certain type:

>>> myvar = "abc"
>>> isinstance(myvar, str)
True
2 Likes