Home > database >  How to make my if statement work with input
How to make my if statement work with input

Time:10-21

x = input("Test: ")
if x == "test: "   1:
    print("test")

I'm simply trying to make this input system with an if statement work, and Google and VS are hating me and I cannot do it.

CodePudding user response:

your first line will set the variable x to have the value of whatever is entered at the prompt. It will not have the value x = 'Test: ' blahblah.

It will also bring in whatever is entered as a string, so if you enter 1 it will be converted to '1'.

Try just doing if x == '1':

CodePudding user response:

An input statement returns the input given so x == "test: " 1: is incorrect and also you did not capitalize your 't'. Your code will look like this when wanting to get the input as 1:

x = int(input("Test: "))
if (x == '1'):
    print("test")

By default the input returns a string so if you type 1 which is a int it will make it into a string. You can use the int() function to turn the string into an int(works only if it is a number/integer, if not it throws error). Here is the code for that:

x = int(input("Test: "))
if (x == 1):
    print("test")
  • Related