0

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

1 回答 1

1

首先,您的函数不返回任何内容。看看添加一个 return 语句来传回一个值。 这是一个相当简单的函数介绍

其次,如果函数确实返回了一个值,您希望将其分配给一个变量,您的代码只是调用该函数而不进行分配。

例如,这个小程序将一个整数传递给一个函数,然后该函数将其加倍并返回新值,然后将其打印出来。

def double(x):
    y = x * 2
    return y

z = double(2)
print(z)
于 2013-02-07T05:03:40.783 回答