Python Forum
Problem with defining a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with defining a function
#1
For some reason my code keeps giving me a syntax error when defining the second function
It will tell me "SyntaxError: invalid syntax" while "def" (for the second function is highlighted in red

>>> counter = 0
>>> PassCounter = 0
>>> FailCounter = 0
>>> Average = 0
#This function does the calculations
def Calc(): 
    MidGrade = int(input("Your mid-term grade is: "))
    print (str(MidGrade) + "%")
    FinGrade = int(input("Your Final grade is: "))
    print (str(FinGrade) + "%")
    WeightedMid = MidGrade*.4
    WeightedFin = FinGrade*.6
    global Average
    Average = WeightedMid + WeightedFin

#This function outputs the necessary information  
def Output():
    print ("Your Average is: " + str(Average) + "%")
    counter = + 1
    if Average > 59.99:
        print ("You have passed the course!")
        global PassCounter
        PassCounter = + 1
    else:
        print ("You have failed the course.")
        global FailCounter
        FailCounter = + 1
    print ("Number of grades calculated: " + str(counter))
    print ("Number of students who have passed: " + str(PassCounter))
    print ("Number of students who have failed: " + str(FailCounter))

print (Calc())
print (Output())
SyntaxError: invalid syntax
[Image: do2iLKo.png]
Reply
#2
Use python tags for multi-line code instead of icode tags. The icode tags are for inline code.

I cannot duplicate that error, either in the interactive interpreter or running from the command line. What operating system and software are you using?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I'm using Python 3.7.0 on Windows 7
Reply
#4
Okay, but that window doesn't look like the Windows command line. Is it some other software?

Does anyone else have 3.7.0 installed that they can test this? I'm still running 3.6.3.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
You are running all code in interactive shell(>>>).
You need to open a New File window,look at this .
Then whole code need to be without >>>.
You save eg grades.py and Run with F5.
counter = 0
PassCounter = 0
FailCounter = 0
Average = 0
#This function does the calculations
def Calc(): 
    MidGrade = int(input("Your mid-term grade is: "))
    print (str(MidGrade) + "%")
    FinGrade = int(input("Your Final grade is: "))
    print (str(FinGrade) + "%")
    WeightedMid = MidGrade*.4
    WeightedFin = FinGrade*.6
    global Average
    Average = WeightedMid + WeightedFin
 
#This function outputs the necessary information  
def Output():
    print ("Your Average is: " + str(Average) + "%")
    counter = + 1
    if Average > 59.99:
        print ("You have passed the course!")
        global PassCounter
        PassCounter = + 1
    else:
        print ("You have failed the course.")
        global FailCounter
        FailCounter = + 1
    print ("Number of grades calculated: " + str(counter))
    print ("Number of students who have passed: " + str(PassCounter))
    print ("Number of students who have failed: " + str(FailCounter))
 
print (Calc())
print (Output())
I may come back with suggestion to fix stuff that are not good in this code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Conjugate Gradient having issues with defining A (function to solve [A]{x} = {b} ) DimosG 2 2,826 Sep-21-2021, 08:32 PM
Last Post: 1968Edwards
  Defining a function with input abcd 5 3,117 Feb-21-2021, 02:34 AM
Last Post: NullAdmin
  When Defining a Function with an Equation as a Default Argument, which Value Is Used? OJGeorge4 4 2,673 Apr-09-2020, 08:48 AM
Last Post: DeaD_EyE
  Problem defining a variable rix 6 3,197 Dec-17-2019, 11:34 AM
Last Post: rix
  A problem with defining variables meru120 5 3,407 Apr-16-2019, 03:13 PM
Last Post: buran
  IDLE indenting 29 columns after defining function Cosmo_Kane 1 2,431 Jun-03-2018, 08:53 AM
Last Post: Larz60+
  Defining a Function mattt1998 4 3,414 Dec-14-2017, 07:02 AM
Last Post: buran
  Defining an fsolve function to call later hegdep 1 3,077 Oct-25-2017, 07:38 AM
Last Post: hegdep
  init vs_init_ while defining method/function? hsunteik 1 3,646 Dec-24-2016, 08:27 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

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