我是编程的新手( python )并且一直在做一些最近的作业,但我真的坚持这个简单的函数实现。本质上,我必须要求在main()中执行用户输入,然后定义另一个函数来计算分配给用户输入的变量的计算。当在IDLE中运行时,它输出0.任何帮助或提示将非常有用,谢谢。这是我到目前为止的代码:
#global variables
firstClass = 0.0
businessClass = 0.0
economyClass = 0.0
soldFirstClass = 0.0
soldBusinessClass = 0.0
soldEconomyClass = 0.0
totalFC = 0.0
totalBC = 0.0
totalEC = 0.0
######################################
###########MAIN FUNCTION#############
######################################
def main():
print('***WELCOME***\n')
print('***Please ENTER the Airlines ticket prices***\n')
firstClass = float(input ('Please ENTER a ticket price for First Class: '))
businessClass = float(input ('Please ENTER a ticket price for Business Class: '))
economyClass = float(input ('Please ENTER a ticket price for Economy Class: '))
print('\nThank You\n')
soldFirstClass = float(input ('Please ENTER the number of sold tickets for First Class: '))
soldBusinessClass = float(input ('Please ENTER the number of sold tickets for Business Class: '))
soldEconomyClass = float(input ('Please ENTER the number of sold tickets for Economy Class: '))
calcClass()
def calcClass():
global firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass
totalFC = firstClass*soldFirstClass
print('Total money earned for First Class = %0.0f ') % totalFC
totalBC = businessClass*soldBusinessClass
print('Total money earned for Business Class = %0.0f ') % totalBC
totalEC = economyClass*soldEconomyClass
print('Total money earned for Economy Class = %0.0f ') % totalEC
main()
答案 0 :(得分:2)
您的main()
正在分配给本地变量。将global …
语句从calcClass()
复制到main()
。
话虽如此,使用全局变量传递信息几乎总是一个坏主意。您应该将它们作为参数传递:
# No globals; just go straight into main
def main():
print('***WELCOME***\n')
print('***Please ENTER the Airlines ticket prices***\n')
firstClass = float(input ('Please ENTER a ticket price for First Class: '))
⋮
calcClass(firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass)
def calcClass(firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass):
totalFC = firstClass*soldFirstClass
print('Total money earned for First Class = %0.0f ') % totalFC
⋮
答案 1 :(得分:2)
代码中的第一个问题是你向后使用global
(这实际上是python中比较混乱的事情之一)。
使用global来声明要写入的全局范围中的哪些变量(您正在使用它来标识要读取的变量)。只要您不写入与全局变量同名的变量,就可以从该全局读取而无需使用全局声明。
因此,您可以对代码进行的最少更改是将正确的全局声明添加到两个函数中,以识别要分配给的全局变量。
然而,正确的要做的是消除您要写入的任何全局变量。而是将参数添加到函数中,并使用参数在它们之间传递值。
答案 2 :(得分:0)
不要使用全局变量。相反,让calcClass
接受参数,如下所示:
totalFC = 0.0
totalBC = 0.0
totalEC = 0.0
def main():
print('***WELCOME***\n')
print('***Please ENTER the Airlines ticket prices***\n')
firstClass = float(input ('Please ENTER a ticket price for First Class: '))
businessClass = float(input ('Please ENTER a ticket price for Business Class: '))
economyClass = float(input ('Please ENTER a ticket price for Economy Class: '))
print('\nThank You\n')
soldFirstClass = float(input ('Please ENTER the number of sold tickets for First Class: '))
soldBusinessClass = float(input ('Please ENTER the number of sold tickets for Business Class: '))
soldEconomyClass = float(input ('Please ENTER the number of sold tickets for Economy Class: '))
calcClass(firstClass, businessClass, economyClass, soldFirstClass, soldBusinessClass, soldEconomyClass)
def calcClass(firstClass, businessClass, economyClass, soldFirstClass, soldBusinessClass, soldEconomyClass):
totalFC = firstClass*soldFirstClass
print('Total money earned for First Class = %0.0f ') % totalFC
totalBC = businessClass*soldBusinessClass
print('Total money earned for Business Class = %0.0f ') % totalBC
totalEC = economyClass*soldEconomyClass
print('Total money earned for Economy Class = %0.0f ') % totalEC
if __name__ == '__main__':
main()
答案 3 :(得分:0)
我认为你犯了一个小错误。
在这个“主函数”模块中,main()
也是一个函数,就像'calcClass()`。
因此,当您想在main()
中使用这些全局变量时,您必须将此语句添加到main函数,就像您对calcClass()
函数所做的那样
global firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass
所以,主要功能如下:
def main():
# NOTE THIS LINE.
global firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass
print('***WELCOME***\n')
print('***Please ENTER the Airlines ticket prices***\n')
firstClass = float(input ('Please ENTER a ticket price for First Class: '))
businessClass = float(input ('Please ENTER a ticket price for Business Class: '))
economyClass = float(input ('Please ENTER a ticket price for Economy Class: '))
print('\nThank You\n')
soldFirstClass = float(input ('Please ENTER the number of sold tickets for First Class: '))
soldBusinessClass = float(input ('Please ENTER the number of sold tickets for Business Class: '))
soldEconomyClass = float(input ('Please ENTER the number of sold tickets for Economy Class: '))
calcClass()
否则,你可以通过声明函数calcClass()
的参数并将这些变量作为参数传递来代替这样做。
这就是代码的样子:
def main():
print('***WELCOME***\n')
print('***Please ENTER the Airlines ticket prices***\n')
firstClass = float(input ('Please ENTER a ticket price for First Class: '))
businessClass = float(input ('Please ENTER a ticket price for Business Class: '))
economyClass = float(input ('Please ENTER a ticket price for Economy Class: '))
print('\nThank You\n')
soldFirstClass = float(input ('Please ENTER the number of sold tickets for First Class: '))
soldBusinessClass = float(input ('Please ENTER the number of sold tickets for Business Class: '))
soldEconomyClass = float(input ('Please ENTER the number of sold tickets for Economy Class: '))
# NOTE THIS LINE
calcClass(firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass)
# NOTE THIS LINE
def (calcClass firstClass, soldFirstClass, businessClass, soldBusinessClass, economyClass, soldEconomyClass):
totalFC = firstClass*soldFirstClass
print('Total money earned for First Class = %f ') % totalFC
totalBC = businessClass*soldBusinessClass
print('Total money earned for Business Class = %f ') % totalBC
totalEC = economyClass*soldEconomyClass
print('Total money earned for Economy Class = %f ') % totalEC
main()