python新手 Windows 7 python 3.3.0 我无法弄清楚如何返回函数参数值。一直在四处寻找疯狂 这是一个课堂作业,但我确实有一个简单的脚本可以上交,但是我的射击是为了推动我的自我。只是需要知道我是否走错了方向。感谢
def main():
seat_a = 15.00
seat_b = 12.00
seat_c = 9.00
class_a(seat_a)
class_b(seat_b)
class_c(seat_c)
set_a = class_a(ticket_a)
set_b = class_b(ticket_b)
set_c = class_b(ticket_c)
result_a_b_c = set_a + set_b + set_c
print("The total income generated from all ticket sales is $ ", format(result_a_b_c,',.2f'))
def class_a(ticket_a):
while True:
try:
ask_a = float(input("How many class A seat tickets were sold? "))
total_a = ticket_a * (ask_a)
break
except ValueError:
print("That is not a valid number. Please try again.")
class_a(ticket_a)
break
total_a = class_a(ticket_a)
def class_b(ticket_b):
while True:
try:
ask_b = float(input("How many class B seat tickets were sold? "))
total_b = ticket_b * (ask_b)
break
except ValueError:
print("That is not a valid number. Please try again.")
class_b(ticket_b)
total_b = class_b(ticket_b)
break
def class_c(ticket_c):
while True:
try:
ask_c = float(input("How many class C seat tickets were sold? "))
total_c = ticket_c * ask_c
break
except ValueError:
print("That is not a valid number. Please try again.")
class_c(ticket_c)
total_c = class_c(ticket_c)
break
main()
答案 0 :(得分:1)
首先,您的功能不会返回任何内容。查看添加return语句以传回值。 Here is a fairly simple introduction to functions
其次,如果函数确实返回了一个值,你想将它分配给一个变量,你的代码只是调用函数而没有赋值。
例如,这个小程序将一个整数传递给一个函数,然后将它加倍并返回新值,然后打印出来。
def double(x):
y = x * 2
return y
z = double(2)
print(z)