Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help
#1
I don't know what the second logical error is. Can anyone help?

1) Please review the Python 3 code snippet below. In this snippet, there are two syntax errors and two logical errors. For your answer, please describe all four issues you found and submit a fixed code snippet that will make running the program run as expected. Print out the correct average of the two numbers that are inputted by the user.

### average.py

def find_average(a, b)
''' find_average will return the average of both a and b, which are floats '''
return x + y / 2

x = float( input("Please enter a number: ") )
y = float( input("Please enter another number: ") )
average = find_average(x y)
print(average)
My answer ~

The correct code is as below:

def find_average(a,b):
   return (a+b)/2
x = float(input("enter a number"))
y = float(input("enter another number"))
average = find_average(x,y)
print (average) 
The bold parts shows the errors present in the question code.
1. Syntax errors:
a.) While defining any function in Python the definition must end with colon ‘:’ sign. Ex: def find_average(a,b):
b.) While calling any function the arguments must be passed with comma (,) in between. Ex: find_average(x,y)

2. Logical error: these errors may not stop the code from running but will not give the desire result.
a.) The mathematical expression of average must contain brackets as shown in the answer code. Ex: (a+b)/2
Reply


Messages In This Thread
Need help - by cwong79 - Jun-02-2018, 09:50 PM
RE: Need help - by Larz60+ - Jun-03-2018, 12:58 AM
RE: Need help - by cwong79 - Jun-03-2018, 02:33 AM
RE: Need help - by buran - Jun-03-2018, 05:56 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020