0% found this document useful (0 votes)
10 views2 pages

Python Input Errors and Fixes Guide

Uploaded by

dsehgal909
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Python Input Errors and Fixes Guide

Uploaded by

dsehgal909
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q27. Neha is not able to add the two numbers entered through the keyboard.

Help her
to find the error(s) in the following code:

num1 = input("enter the first number")


num2 = input("enter the second number")
c = num1 + num2
print(c)

Error:

 input() returns string by default. Adding strings will concatenate, not perform
arithmetic.

Correct Code:

num1 = int(input("enter the first number: "))


num2 = int(input("enter the second number: "))
c = num1 + num2
print(c)

Q28. What will be the output when the following code is executed:

my_text = ""
print(my_text)

Answer:
The output will be a blank line, since my_text is an empty string.

Q29. Make changes in the following statement to print the output in same line (don’t
change the text):

print("Hello ")
print("World")

Correct Code:

print("Hello ", end="")


print("World")

Output:

Hello World

Q30. Rita is trying to enter her marks of IP: 67.5 through the keyboard. Python is
giving the error. Help her to correct the statement:
Marks = input("Enter the first marks:")

Error:

 input() always returns a string. For decimal numbers, must convert to float.

Correct Code:

Marks = float(input("Enter the first marks: "))

Q31. Get the name, salary and company of an employee and print in the following
format:
Output format:

Name Salary Company


Jessa 8000 Google

Correct Code:

name = "Jessa"
salary = 8000
company = "Google"

print("Name\tSalary\tCompany")
print(name, salary, company, sep="\t")

Q32. Recognize the type of error:

a. Using integer division instead of floating-point division → Logical Error


b. Leaving out a symbol (colon, comma, brackets) → Syntax Error
c. Misspelling a keyword → Syntax Error
d. Performing an operation on incompatible types → Runtime Error (TypeError)
e. Using an identifier which has not been defined → Runtime Error (NameError)
f. Getting operator precedence wrong → Logical Error
g. Making a mistake in a boolean expression → Logical Error
h. Leaving out a keyword → Syntax Error
i. Putting a keyword in the wrong place → Syntax Error
j. Incorrect indentation → Indentation Error (Syntax Error)
k. Empty block → Syntax Error (IndentationError / expected statement)
l. Accessing a list/dict/object attribute which doesn’t exist → Runtime Error (IndexError /
KeyError / AttributeError)
m. Trying to access a file which doesn’t exist → Runtime Error (FileNotFoundError)

You might also like