Section 1.4 - IT 140_ Introduction to Scripting _ zyBooks

pdf

School

Southern New Hampshire University *

*We aren’t endorsed by this school

Course

140 - X625

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

4

Uploaded by DeaconCaterpillar4154

Report
Students: Section 1.4 is a part of 1 assignment: 1-3 zyBooks Participation Activities Includes: PA 1.4 Errors Syntax errors As soon as a person begins trying to program, that person will make mistakes. One kind of mistake, known as a syntax error , is to violate a programming language's rules on how symbols can be combined to create a program. An example is putting multiple prints on the same line. The interpreter will generate a message when encountering a syntax error. The error message will report the number of the offending line, in this case 7, allowing the programmer to go back and ±x the problem. Sometimes error messages can be confusing, or not particularly helpful. Below, the message "invalid syntax" is not very precise, but is the best information that the interpreter is able to report. With enough practice a programmer becomes familiar with common errors and is able to avoid them, avoiding headaches later. Note that syntax errors are found before the program is ever run by the interpreter. In the example below, none of the prints prior to the error is in the output. Figure 1.4.1: A program with a syntax error. print ( 'Current salary is' , end = ' ' ) print ( 45000 ) print ( 'Enter new salary:' , end = ' ' ) new_sal = int ( input ()) print ( new_sal ) print ( user_num ) File "<main.py>", line 7 print(new_sal) print(user_num) ^ SyntaxError: invalid syntax PARTICIPATION ACTIVITY 1.4.1: Syntax errors. Find the syntax errors. Assume variable num_dogs exists. 1) print(num_dogs). Error No Error 2) print("Dogs: " num_dogs) Error No Error 3) print('Woof!") Error No Error 4) print(Woof!) Error No Error 5) print("Hello + friend!") Error No Error PARTICIPATION ACTIVITY 1.4.2: Common syntax errors. Find and click on the 3 syntax errors. Feedback? Feedback?
1) triangle_base = 0 # Triangle base (cm) triangle_height = 0 # Triangle height (cm) # Triangle area (cm) print('Enter triangle base (cm): ') print('Enter triangle height (cm): ') triangle_area = (triangle_base * triangle_height) / 2 print(triangle_base) print(') / 2 = ', end=' ') print(triangle_area, end=' ') Good coding practice New programmers will commonly write programs with many syntax errors, leading to many frustrating error messages. To avoid continually encountering error messages, a good practice is to execute the code frequently, writing perhaps a few (3–5) lines of code and then ±xing errors, then writing a few more lines and running again and ±xing errors, and so on. Experienced programmers may write more lines of code each time, but typically still run and test syntax frequently. PARTICIPATION ACTIVITY 1.4.3: Run code frequently to avoid many errors. PARTICIPATION ACTIVITY 1.4.4: Testing for syntax errors. 1) Experienced programmers write an entire program before running and testing the code. True False Runtime errors The Python interpreter is able to detect syntax errors when the program is initially loaded, prior to actually executing any of the statements in the code. However, just because the program loads and executes does not mean that the program is correct. The program may have another kind of error called a runtime error , wherein a program's syntax is correct but the program attempts an impossible operation, such as dividing by zero or multiplying strings together (like 'Hello' * 'ABC'). A runtime error halts the execution of the program. Abrupt and unintended termination of a program is often called a crash of the program. Consider the below program that begins executing, prints the salary, and then waits for the user to enter an integer value. The int() statement expects a number to be entered, but gets the text 'Henry' instead. Figure 1.4.2: Runtime errors can crash the program. The program crashes because the user enters 'Henry' instead of an integer value. print ( 'Salary is' , end = ' ' ) print ( 20 * 40 * 50 ) print ( 'Enter integer: ' , end = ' ' ) user_num = int ( input ()) print ( user_num ) Salary is 40000 Enter integer: Henry Traceback (most recent call last): File "<stdin>", line 5, in <module> ValueError: invalid literal for int() with base 10: 'Henry' triangle_area = 0 triangle_base = int(input() triangle_height = int(input()) # Calculate triangle area Print out the triangle base, height, and area print('Triangle area = (', end=' ') print(*, end=' ') print(triangle_height, end=' ') print('cm**2') Feedback? Run code Run code Run code Run code stmt1 stmt2 stmt3 stmt4 stmt5 stmt6 stmt7 stmt1 stmt2 stmt3 stmt4 stmt5 stmt6 stmt7 1. Writing many lines of code without compiling is bad practice. 2. New programmers should compile their program after every couple of lines. Captions Feedback? Feedback? Start 2x speed
Runtime errors are categorized into types that describe the sort of error that has occurred. Above, a ValueError occurred, indicating that the wrong sort of value was passed into the int() function. Other examples include a NameError and a TypeError, both described in the table below. Common error types Table 1.4.1: Common error types. Error type Description SyntaxError The program contains invalid code that cannot be understood. IndentationError The lines of the program are not properly indented. ValueError An invalid value is used – can occur if giving letters to int(). NameError The program tries to use a variable that does not exist. TypeError An operation uses incorrect types – can occur if adding an integer to a string. PARTICIPATION ACTIVITY 1.4.5: Match the lines of code with the error type that they produce. Match the following lines of code with the correct error type. Assume that no variables already exist. If unable to drag and drop, refresh the page. lyric = 99 + " bottles of pop on the wall" print("Friday, Friday") int("Thursday") day_of_the_week = Friday print('Today is Monday") Logic errors Some errors may be subtle enough to silently misbehave, instead of causing a runtime error and a crash. An example might be if a programmer accidentally typed "2 * 4" rather than "2 * 40" – the program would load correctly, but would not behave as intended. Such an error is known as a logic error , because the program is logically ²awed. A logic error is often called a bug . Figure 1.4.3: The programmer made a mistake that happens to be correct syntax, but has a different meaning. The below program attempts to calculate a 5% raise for an employee's salary. The programmer made a mistake by assigning raise_percentage to 5, instead of 0.05, thus giving a happy employee a 500% raise. current_salary = int ( input ( 'Enter current salary:' )) raise_percentage = 5 # Logic error gives a 500% raise instead of 5%. new_salary = current_salary + ( current_salary * raise_percentage ) print ( 'New salary:' , new_salary ) Enter current salary: 10000 New salary: 60000 The programmer clearly made an error, but the code is actually correct syntax – it just has a different meaning than was intended. So the interpreter will not generate an error message, but the program's output is not what the programmer expects – the new computed salary is much too high. These mistakes can be very hard to debug. Paying careful attention and running code after writing just a few lines can help avoid mistakes. zyDE 1.4.1: Fix the bug. Click run to execute the program and note the incorrect program output. Fix the bug in the program. Feedback? Feedback? NameError ValueError TypeError IndentationError SyntaxError Reset Feedback? Feedback?
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Activity summary for assignment: 1-3 zyBooks Participation Activities 98 % 98 % submitted to desire2learn Figure 1.4.4: The ±rst bug. A sidenote: The term "bug" to describe a runtime error was popularized in 1947. That year, engineers working with pioneering computer scientist Grace Hopper discovered their program on a Harvard University Mark II computer was not working because a moth was stuck in one of the relays (a type of mechanical switch). They taped the bug into their engineering log book, which is still preserved today ( http://en.wikipedia.org/wiki/Computer_bug ). CHALLENGE ACTIVITY 1.4.1: Basic syntax errors. Retype the statements, correcting the syntax error in each print statement. print('Predictions are hard.") print(Especially about the future.) user_num = 5 print('user_num is:' user_num) Learn how our autograder works 553398.3976864.qx3zqy7 How was this section? | Load default template... num_beans = 500 num_jars = 3 total_beans = 0 print ( num_beans , 'beans in' , end = ' ' ) print ( num_jars , 'jars yields' , end = ' ' ) total_beans = num_beans * num_jars print ( 'total_beans' , 'total' ) Run Feedback? Feedback? ''' Your solution goes here ''' Run View your last submission Feedback? Provide section feedback Completion details 1 2 3 4 5 6 7 8 9 1 2 3