python-3.x 试图从另一个程序导入公式[重复]

wn9m85ua  于 2023-03-31  发布在  Python
关注(0)|答案(2)|浏览(140)

此问题在此处已有答案

python using variables from another file(2个答案)
3天前关闭。
当我运行程序时,我的导入不起作用,我尝试了不同的方法,但我得到相同的错误:
“名称错误:未定义名称'SINGLE 1'”
这是我正在运行的程序:

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()

我正在尝试导入的另一个程序是这样的:

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)

当我最初在第一个程序中有它的时候,它工作得很好。谢谢你,我很感激任何帮助!

wj8zmpe1

wj8zmpe11#

你需要在你的函数中定义参数,这样它们就可以接收你在第一个文件中定义的参数:

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)

在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

你跑(我不知道任何关于棒球所以...):

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

--希望这能有所帮助

du7egjpx

du7egjpx2#

SINGLE1未在函数内部定义。
我只能猜测,但似乎你没有创建所需的Global variables
您可以将SINGLE1变量(和其他变量)传递到函数中,而不是创建全局变量。

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)

相关问题