这里我有这个代码来计算弹跳球的行进距离。问题在于功能显示摘要及其后面的3个功能。在第三行,当我定义calculateTotalDistIn()时,我意识到函数不知道变量bounceNum是什么或跟随它的任何其他变量。有没有什么办法可以在不使用全局变量的情况下以某种方式发送变量值?谢谢。
def askName(name):
yourName = name.upper()
return yourName
def displaySummary():
calculateTotalDistin()
print("Bouncy index % given was: ", str(index))
print("Start height given was: ", str(height))
def programmerID():
print("\n")
print("Bertrand Zhu")
print("Period 7")
print("Roster#: 20")
print("\n")
def displaySummary():
index = float(input("What is your bouncy Index % ? "))
newIndex = index/100
height = int(input("What is your starting height in inches? "))v\
bounceNum = int(input("How many bounces would you like? "))
distance = calculateTotalDistIn()
distanceFT = calculateTotalDistFT()
distanceCT = calculateTotalDistCT()
print("Bouncy Index % given was " + str(index))
print("Start Height given was " + str(height))
print("Bounce Count Given was " + str(bounceNum))
print("\n")
print("Distance Inches: " + distance)
print("Distance Feet: " + distanceFT)
print("Distance Centimeters " + distanceCT)
def calculateTotalDistIn():
distance = 0
count = 0
while count <= bounceNum:
modHeight = height * newIndex
distance += modHeight
count = 1
height = modHeight
return distance
def calculateTotalDistFT():
distance = 0
count = 0
while count <= bounceNum:
modHeight = height * newIndex
distance += modHeight
count = 1
height = modHeight
distFT = float(distance/12)
return distFT
def calculateTotalDistCT():
distance = 0
count = 0
while count <= bounceNum:
modHeight = height * newIndex
distance += modHeight
count = 1
height = modHeight
distCT = float(distance*(2.54))
return distCT
def goodbye(varX):
print("Goodbye, " + varX)
def main():
while True:
name = input("What is your name? ")
yourName = askName(name)
if yourName == "QUIT":
break
else:
programmerID()
displaySummary()
goodbye()
#Run the Program
main()
答案 0 :(得分:2)
您有两个选择:一个是将值作为参数传递,另一个是为每个对象类别定义一个类,并让它们操纵自己的数据。我将举例说明选项1:
def displaySummary():
index = float(input("What is your bouncy Index % ? "))
newIndex = index/100
height = int(input("What is your starting height in inches? "))v\
bounceNum = int(input("How many bounces would you like? "))
distance = calculateTotalDistCT(height, newIndex, bounceNum)
...
def calculateTotalDistCT(height, newIndex, bounceNum):
distance = 0
count = 0
while count <= bounceNum:
modHeight = height * newIndex
distance += modHeight
count = 1
height = modHeight
distCT = distance * 2.54
return distCT
您可以自由查找许多OOP教程。
答案 1 :(得分:2)
您应该将想要在方法中使用的变量作为参数传递。
例如:
def displaySummary():
bounceNum = int(input("How many bounces would you like? "))
newIndex = index/100
height = int(input("What is your starting height in inches? "))
calculateTotalDistin(bounceNum, height, newIndex)
print("Bouncy index % given was: ", str(index))
print("Start height given was: ", str(height))
def calculateTotalDistIn(bounceNum, height, newIndex):
distance = 0
count = 0
while count <= bounceNum:
modHeight = height * newIndex
distance += modHeight
count = 1
height = modHeight
return distance
但是,我会建议您使用类,而不是这样做。然后,您可以拥有更好的结构方法和代码。