Not being able to make a code for taking an input

I was unable to create a programme as directed in the video of the first module taking an input for python. There is some error occurring in the same despite me rectifying it multiple times thanks

Hi @avantikay1

ValueError: When running the code, the input function doesn’t prompt correctly in some environments like Replit, leading to the “invalid literal for int()” error. It could be related to how the input is provided.

Below is the the corrected code.

temp = input("Enter temperature here: ")
temp = int(temp)

if temp > 30:
    print("It is hot")
else:
    print("It is cold")

Do try this and let us know. If you still face any issue, please feel free to get back to us

Hello, it appears that you have just missed using input function in the very first line of code.
Just change the statement

temp = ("Enter temperature here: ")

to

temp = input("Enter temperature here: ")

This shall fix the code. Happy coding!

Hi @avantikay1

The issue you’re experiencing with the input function and the “invalid literal for int()” error typically arises when the input provided is not a valid integer. Here’s a revised version of your code that includes error handling to address this problem:

try:
    temp = input("Enter temperature here: ")
    temp = int(temp)

    if temp > 30:
        print("It is hot")
    else:
        print("It is cold")
except ValueError:
    print("Please enter a valid integer for the temperature.")

Explanation:

  1. Input Handling:
  • The input function is correctly used to prompt the user to enter a temperature value.
  1. Conversion to Integer:
  • temp = int(temp) attempts to convert the input string to an integer.
  1. Error Handling:
  • A try block is used to catch any ValueError that occurs if the input cannot be converted to an integer.
  • The except ValueError block prints an error message if the conversion fails, prompting the user to enter a valid integer.

Feel free to test this updated code, and if you encounter any further issues, please let us know!

Hi @avantikay1, you have not used the ‘input()’ function to get the temperature.
You can either use:

temp = input("Enter temperature here: ")
temp = int(temp)

or you can combine it into a single statement:

temp = int(input("Enter temperature here: "))