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
#2
first code snippet line 5 not indented
Reply
#3
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
Reply
#4
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

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