runtime: php
env: flex
runtime_config:
document_root: web
beta_settings:
cloud_sql_instances: "<Project ID>:<Location>:<Instance>"
这是你可以在¨values_average函数中看到的东西,当我试图通过在if语句之后调用“主函数”来循环整个事情时它不会要求我输入新的CHANGE_TOTAL价值,这是我想做的。任何建议?提前谢谢
答案 0 :(得分:0)
我认为依赖可变的全局状态是 uncool 。但如果你真的想要的话,你可以轻松地这样做。移动您的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循环而不是相互递归。
答案 1 :(得分:0)
我稍微修改了你的代码。编程中的好习惯是不要定义它们将要改变的全局变量,因为它在大型协议中几乎不可能跟踪错误。最好将要更改的值传递给函数,然后返回其新值。这使得代码库更容易测试各个函数的错误。如果您想要返回多个值,只需返回元组并解压缩,例如
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除了块,如果你意外错误输入一个值会使程序保持打开状态,这是非常好的做法。
#!/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()