调用函数而不遗漏输入全局变量


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 语句之后调用 ̈main 函数 ̈ 来循环整个事情时,它不会要求我输入新的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-loop 而不是相互递归。

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

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

相关内容

  • 没有找到相关文章

最新更新