Trying to import a formula from another program
Question:
When I run the program my import does not work, I have tried different methods but I get the same error:
"NameError: name ‘SINGLE1’ is not defined"
This is the program I am running:
name="What is the name of the player?:"
NAME1=input(name)
atbats=("Number of At-Bats for ")+(NAME1)+"?:"
ATBATS1=(int(input(atbats)))
single=("Number of Singles for ")+(NAME1)+("?:")
SINGLE1=(int(input(single)))
double=("Number of Doubles for ")+(NAME1)+("?:")
DOUBLE1=(int(input(double)))
triple=("Number of Triples for ")+(NAME1)+("?:")
TRIPLE1=(int(input(triple)))
home=("Number of Homeruns for ")+(NAME1)+("?:")
HOME1=(int(input(home)))
from BBFUN import *
print("Batting Average for ",(NAME1),(":"))
batting_average()
print("Slugging Percentage for ")+(NAME1)+(":")
slugging_percent()
The other program I am trying to import is this:
def batting_average():
avg=(SINGLE1)+(DOUBLE1)+(TRIPLE1)+(HOME1)
bat_avg=format(((avg)/ (ATBATS1)),'.2f')
return((bat_avg))
def slugging_percent():
sing=(SINGLE1)*1
doub=(DOUBLE1)*2
trip=(TRIPLE1)*3
homerun=(HOME1)*4
slug_total=(sing)+(doub)+(trip)+(homerun)
slug_avg=format(((slug_total)/ (ATBATS1)),'.2f')
return(slug_avg)
When I originally had it in the first program it works fine. Thank you I appreciate any help!
Answers:
you need to define parameters in your functions so that they can receive the arguments you’ve defined in the first file:
from BBFUN import batting_average, slugging_percent
name = "What is the name of the player?:n"
NAME1 = input(name)
atbats = "Number of At-Bats for " + NAME1 + "?:n"
ATBATS1 = (int(input(atbats)))
single = "Number of Singles for " + NAME1 + "?:n"
SINGLE1 = (int(input(single)))
double = "Number of Doubles for " + NAME1 + "?:n"
DOUBLE1 = (int(input(double)))
triple = "Number of Triples for " + NAME1 + "?:n"
TRIPLE1 = (int(input(triple)))
home = "Number of Homeruns for " + NAME1 + "?:n"
HOME1 = (int(input(home)))
bat_avg = batting_average(SINGLE1, DOUBLE1, TRIPLE1, HOME1, ATBATS1)
print("Batting Average for " + NAME1 + ":" + bat_avg)
slug_avg = slugging_percent(SINGLE1, DOUBLE1, TRIPLE1, HOME1, ATBATS1)
print("Slugging Percentage for " + NAME1 + ":" + slug_avg)
then in BBFUN:
def batting_average(single, double, triple, home, atbats):
avg = single + double + triple + home
bat_avg = format((avg / atbats), '.2f')
return bat_avg
def slugging_percent(single, double, triple, home, atbats):
sing = single * 1
doub = double * 2
trip = triple * 3
homerun = home * 4
slug_total = sing + doub + trip + homerun
slug_avg = format((slug_total / atbats), '.2f')
return slug_avg
that you run (I don’t know anything about baseball so…):
What is the name of the player?:
harriet
Number of At-Bats for harriet?:
5
Number of Singles for harriet?:
4
Number of Doubles for harriet?:
3
Number of Triples for harriet?:
2
Number of Homeruns for harriet?:
2
Batting Average for harriet:2.20
Slugging Percentage for harriet:4.80
–hope this helps
SINGLE1 is not defined inside the function.
I can just guess but it seems you are not creating the required Global variables.
Instead of creating global variables, you could pass the SINGLE1 variable (and the others) into your function.
print("Slugging Percentage for ")+(NAME1)+(":")
slugging_percent(SINGLE1)
def slugging_percent(SINGLE1):
sing=(SINGLE1)*1
doub=(DOUBLE1)*2
trip=(TRIPLE1)*3
homerun=(HOME1)*4
slug_total=(sing)+(doub)+(trip)+(homerun)
slug_avg=format(((slug_total)/ (ATBATS1)),'.2f')
return(slug_avg)
When I run the program my import does not work, I have tried different methods but I get the same error:
"NameError: name ‘SINGLE1’ is not defined"
This is the program I am running:
name="What is the name of the player?:"
NAME1=input(name)
atbats=("Number of At-Bats for ")+(NAME1)+"?:"
ATBATS1=(int(input(atbats)))
single=("Number of Singles for ")+(NAME1)+("?:")
SINGLE1=(int(input(single)))
double=("Number of Doubles for ")+(NAME1)+("?:")
DOUBLE1=(int(input(double)))
triple=("Number of Triples for ")+(NAME1)+("?:")
TRIPLE1=(int(input(triple)))
home=("Number of Homeruns for ")+(NAME1)+("?:")
HOME1=(int(input(home)))
from BBFUN import *
print("Batting Average for ",(NAME1),(":"))
batting_average()
print("Slugging Percentage for ")+(NAME1)+(":")
slugging_percent()
The other program I am trying to import is this:
def batting_average():
avg=(SINGLE1)+(DOUBLE1)+(TRIPLE1)+(HOME1)
bat_avg=format(((avg)/ (ATBATS1)),'.2f')
return((bat_avg))
def slugging_percent():
sing=(SINGLE1)*1
doub=(DOUBLE1)*2
trip=(TRIPLE1)*3
homerun=(HOME1)*4
slug_total=(sing)+(doub)+(trip)+(homerun)
slug_avg=format(((slug_total)/ (ATBATS1)),'.2f')
return(slug_avg)
When I originally had it in the first program it works fine. Thank you I appreciate any help!
you need to define parameters in your functions so that they can receive the arguments you’ve defined in the first file:
from BBFUN import batting_average, slugging_percent
name = "What is the name of the player?:n"
NAME1 = input(name)
atbats = "Number of At-Bats for " + NAME1 + "?:n"
ATBATS1 = (int(input(atbats)))
single = "Number of Singles for " + NAME1 + "?:n"
SINGLE1 = (int(input(single)))
double = "Number of Doubles for " + NAME1 + "?:n"
DOUBLE1 = (int(input(double)))
triple = "Number of Triples for " + NAME1 + "?:n"
TRIPLE1 = (int(input(triple)))
home = "Number of Homeruns for " + NAME1 + "?:n"
HOME1 = (int(input(home)))
bat_avg = batting_average(SINGLE1, DOUBLE1, TRIPLE1, HOME1, ATBATS1)
print("Batting Average for " + NAME1 + ":" + bat_avg)
slug_avg = slugging_percent(SINGLE1, DOUBLE1, TRIPLE1, HOME1, ATBATS1)
print("Slugging Percentage for " + NAME1 + ":" + slug_avg)
then in BBFUN:
def batting_average(single, double, triple, home, atbats):
avg = single + double + triple + home
bat_avg = format((avg / atbats), '.2f')
return bat_avg
def slugging_percent(single, double, triple, home, atbats):
sing = single * 1
doub = double * 2
trip = triple * 3
homerun = home * 4
slug_total = sing + doub + trip + homerun
slug_avg = format((slug_total / atbats), '.2f')
return slug_avg
that you run (I don’t know anything about baseball so…):
What is the name of the player?:
harriet
Number of At-Bats for harriet?:
5
Number of Singles for harriet?:
4
Number of Doubles for harriet?:
3
Number of Triples for harriet?:
2
Number of Homeruns for harriet?:
2
Batting Average for harriet:2.20
Slugging Percentage for harriet:4.80
–hope this helps
SINGLE1 is not defined inside the function.
I can just guess but it seems you are not creating the required Global variables.
Instead of creating global variables, you could pass the SINGLE1 variable (and the others) into your function.
print("Slugging Percentage for ")+(NAME1)+(":")
slugging_percent(SINGLE1)
def slugging_percent(SINGLE1):
sing=(SINGLE1)*1
doub=(DOUBLE1)*2
trip=(TRIPLE1)*3
homerun=(HOME1)*4
slug_total=(sing)+(doub)+(trip)+(homerun)
slug_avg=format(((slug_total)/ (ATBATS1)),'.2f')
return(slug_avg)