Search code examples
pythontype-conversionraw-input

Python Typecasting


I have taken a number as a raw_input and stored it in a variable port. However, I have to use this number(stored as a string) as a parameter in an "if" statement. I cannot take the number as "input" as I require that number as a string later. I tried typecasting the string to an int but it is not working. How do I use the raw_input as a number?

port = raw_input("Enter the port no:")
temp = int(port)

if (temp >> 0 or temp << 323):


Solution

  • I think your problem is in your comparison in your if statment, change temp >> 0to temp > 0

    Or, what you want is a try / catch or a try / except ValueError like :

    port = raw_input("Enter the port no:")
    try:
      temp = int(port)
    except ValueError:
      print("That's not an int! Please provide a valid port")
    
     if (0 < temp < 323):