Why is raw_input not defined in python?

Python 3 has replaced Python 2’s raw_input[] method with the input[] method. If you try to use raw_input[] in Python 3, you’ll encounter the NameError: name ‘raw_input’ is not defined error.

In this guide, we’re going to discuss what this error means and why you may encounter it. We’ll walk through an example of this error, with a solution, so you can learn how to solve the error.

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

Select your interest
First name

Last name

Email

Phone number

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

NameError: name ‘raw_input’ is not defined

The raw_input[] function in Python 2 collects an input from a user. This input can be converted to any data type, such as a string, an integer, or a floating-point number.

Consider this code:

username = raw_input[“Enter a username: ”]

We can use this code to collect a username from a user in Python 2.

Being able to collect an input from a user means you can make your programs interactive. You don’t just need to define all the data in the program you are going to use. You can ask a user to provide some data.

In Python 3, many changes have been made to the Python language. Among them is raw_input[] was renamed to input[]. Both functions collect a piece of data from sys.stdin [also known as “standard input”] and return that data to a program.

An Example Scenario

We’re going to build a program that calculates the grade a student has earned on their art assignment. The assignment is out of 50 and students can receive either an A, B, C, or Fail grade. To start, let’s ask our user to insert a grade whose letter grade we will calculate:

numerical_grade = int[raw_input[“Enter a grade: ”]]

We use raw_input[] to collect a grade from the user. The user must enter a grade into our program before the rest of our program runs. We convert the value a user enters to an integer so we can perform numerical comparisons later in our code. This is because raw_input[] returns a string by default.

We’re going to use an if statement to calculate the corresponding letter grade:

if numerical_grade > 40:
	grade = "A"
elif numerical_grade > 30:
	grade = "B"
elif numerical_grade > 25:
	grade = "C"
else:
	grade = "Fail"

We use one if, two elif, and one else statement to calculate the letter grade a student has earned based on the numerical grade the user has inserted into the program.

Our final step is to print out a message to the console informing the user of the results of our calculation:

print["This student earned a {} grade with a score of {}.".format[grade, numerical_grade]]

This statement will display both the letter and the numerical grade that a student has earned.

Let’s run our code and see what happens:

Traceback [most recent call last]:
  File "main.py", line 1, in 
	numerical_grade = int[raw_input["Enter a grade: "]]
NameError: name 'raw_input' is not defined

Our code returns an error.

The Solution

The error message tells us we are referencing a value that does not exist. Because we are using Python 3.x to run our program, raw_input[] does not exist.

To fix our code, we need to replace our raw_input[] statement with an input[] statement:

numerical_grade = int[input["Enter a grade: "]]

Both the raw_input[] and input[] statements are functionally the same. This means we do not need to make any further changes to our code to make our codebase compatible with Python 3.x.

Let’s run our program with this change made:

Enter a grade: 33
This student earned a B grade with a score of 33.

Our code successfully calculates a student’s grade.

A Note on Reassigning the raw_input Function

A solution that technically works is to assign the value of raw_input[] to the input[] function. We can do this using variable assignment. This will let you use a function called raw_input[] in your Python 3 code.

Consider this example:

raw_input = input

This statement tells Python that the value of raw_input[] should be equal to input[].

This is not a good solution because the official Python 3 documentation phased out the name raw_input[] in favor of input[]. Some developers who read your code may be confused if they see raw_input[] in a Python 3 codebase, thereby slowing down development time.

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

Conclusion

The NameError: name ‘raw_input’ is not defined error is raised when you try to use the raw_input[] method in Python 3. To fix this error, replace all instances of raw_input[] with the input[] function in your program.

Now you have the knowledge you need to fix this error like a professional Python coder!

How do you fix raw_input is not defined?

The Python "NameError: name 'raw_input' is not defined" occurs when we use the raw_input[] function in Python 3. To solve the error, use the input[] function instead of raw_input in Python 3 applications, e.g. s = input['Your name: '] .

How do I define raw_input in Python?

Python raw_input function is used to get the values from the user. We call this function to tell the program to stop and wait for the user to input the values. It is a built-in function. The input function is used only in Python 2.

How do I fix NameError is not defined in Python?

Conclusion # The Python "NameError: function is not defined" occurs when we try to call a function that is not declared or before it is declared. To solve the error, make sure you haven't misspelled the function's name and call it after it has been declared.

Does python3 have raw_input?

The raw_input[] function can read a line from the user. This function will return a string by stripping a trailing newline. It was renamed to input[] function in Python version 3.0 and above.

Chủ Đề