在不遗漏输入全局变量的情况下调用函数

call a function without leaving out input global variable

CHANGE_TOTAL = (float(input('type in the change total ')))
def main():
    value_1 = (float(input('type in the first value ')))
    value_2 = (float(input('type in the second value ')))
    value_3 = (float(input('type the third value ')))
    values_average(value_1,value_2,value_3)
def values_average(a,b,c):
    total = (a+b+c)
    if total >  CHANGE_TOTAL:
        print ('there\'s not change availibility at the moment plase wait')
        main()
    else:
        print ('your change is being process plase wait a moment')
    change_due(total)
def change_due(items_cost):
    input ('"press enter"')
    money_received = (float(input('type in the amount of money received ')))
    change = (money_received - items_cost)
    print ('your change is', change)
    change_total_aft_trans(change)
def change_total_aft_trans(a):
    change_left = (CHANGE_TOTAL - a)
    print ('the change left is', change_left)

main()

这就是您在“values_average 函数”中看到的内容,当我尝试通过在 if 语句之后调用“主函数”来循环整个过程时,它没有询问我输入一个新的 CHANGE_TOTAL 值,这是我想要做的。有什么建议吗?提前致谢

我认为依赖可变的全局状态是 不酷。但如果你真的愿意,你可以很容易地那样做。将你的 CHANGE_TOTAL = (float(input('type in the change total '))) 移到 main 中并放置一个 global 指令:

def main():
    global CHANGE_TOTAL
    CHANGE_TOTAL = (float(input('type in the change total ')))
    value_1 = (float(input('type in the first value ')))
    value_2 = (float(input('type in the second value ')))
    value_3 = (float(input('type the third value ')))
    values_average(value_1,value_2,value_3)
def values_average(a,b,c):
    total = (a+b+c)
    if total >  CHANGE_TOTAL:
        print ('there\'s not change availibility at the moment plase wait')
        main()
    else:
        print ('your change is being process plase wait a moment')
    change_due(total)
def change_due(items_cost):
    input ('"press enter"')
    money_received = (float(input('type in the amount of money received ')))
    change = (money_received - items_cost)
    print ('your change is', change)
    change_total_aft_trans(change)
def change_total_aft_trans(a):
    change_left = (CHANGE_TOTAL - a)
    print ('the change left is', change_left)

我认为这是一个更合理的设计:

def main():
    change_total, *values = prompt_values()
    cost = sum(values)
    while cost > change_total:
        print("there's not change availibility at the moment plase wait")
        change_total, *values = prompt_values()
        cost = sum(values)
    change_due(cost, change_total)

def prompt_values():
    change_total = float(input('type in the change total '))
    value_1 = float(input('type in the first value '))
    value_2 = float(input('type in the second value '))
    value_3 = (float(input('type the third value ')))
    return change_total, value_1, value_2, value_3

def change_due(items_cost, change_total):
    print ('your change is being process plase wait a moment')
    input ('"press enter"')
    money_received = (float(input('type in the amount of money received ')))
    change = (money_received - items_cost)
    print ('your change is', change)
    change_total_aft_trans(change, change_total)

def change_total_aft_trans(change, change_total):
    change_left = change_total - change
    print ('the change left is', change_left)

注意,我使用了 while 循环而不是相互递归。

我已经稍微修改了你的代码。在编程中最好不要定义全局变量,因为它们将要更改,因为在大型代码库中几乎不可能跟踪错误。最好将要更改的值传递给一个函数,然后 return 返回它的新值。这使得代码库更容易测试单个函数的错误。如果您想要 return 多个值,只需 return 元组并解压,例如

def func(val1, val2, val3):
    val1 += 1
    val2 += 2
    val3 += 3 
    return (val1, val2, val3)

def main():
    a, b, c = 1, 2 ,3
    a, b, c = func(a, b, c)
    # a=2, b=4, c=6

我还添加了一个 try except 块,如果您不小心输入了错误的值,它将使程序保持打开状态,这确实是个好习惯。

#!/usr/bin/env python
#-*- coding: utf-8 -*-


def main():
    # Insdead of gloval funciton just pass it to functions
    CHANGE_TOTAL = get_value('CHANGE TOTAL')   

    value_1 = get_value('Value1')
    value_2 = get_value('Value2')
    value_3 = get_value('Value3')
    average = values_average(value_1,value_2,value_3)
    new_balance = withdraw(CHANGE_TOTAL, average)

# Since you are getting three float values from input why not make a fucntion
# that gets a values, specifed by a name and checks for invalid entries
# if you ever need to get another float value, just call this with a param
def get_value(name_of_value: str) -> float:
    print('Please enter the',name_of_value, 'value: ', end='')
    while True:
        try:
            value = float(input())
            break
        except ValueError:
            print('Invalid entry, try again: ', end='')
    return value

#this functions does too much seperate it fucntions: get_average() and
# withdraw() deposit()

def values_average(a,b,c):
    total = a + b + c 
    return total / 3 #average

def withdraw(available_change, amount):
    if amount >= available_change:
        return available_change - ammount
    else:
        print('Invaild funds for transaction')

"""
# Please note I have stopped here!! b
def change_due(items_cost):
    input ('"press enter"')
    money_received = (float(input('type in the amount of money received ')))
    change = (money_received - items_cost)
    print ('your change is', change)
    change_total_aft_trans(change)

def change_total_aft_trans(a):
    change_left = (CHANGE_TOTAL - a)
    print ('the change left is', change_left)
"""

main()