Python Forum

Full Version: Need help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
first code snippet line 5 not indented
Actually, the first code line is indented. I meant to show it that way.
(Jun-03-2018, 12:58 AM)Larz60+ Wrote: [ -> ]first code snippet line 5 not indented
on line 5 you are using variables x and y, but these are not defined. You should use a and b (the parameters of the find_average function). See line 2 of the second snippet.