2017-06-12 2 views
1
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() 

c'est la chose que vous pouvez voir dans le funtion ¨values_average lorsque je tente de boucler le tout sur en appelant le function¨after la PRINCIPAuX instruction if il ne me demande pas de taper une nouvelle valeur CHANGE_TOTAL, ce que je voudrais faire. aucun conseil ? merci d'avanceappeler une fonction sans laisser à l'entrée variable globale

+0

Veuillez indenter votre code correctement. Il est impossible de lire Python sans indentation correcte. –

+0

je voulais dire CHANGE_TOTAL –

+0

Ne postez pas de corrections dans les commentaires. * Corrigez la question elle-même en l'éditant *. –

Répondre

0

Je pense que s'appuyer sur l'état global mutable est uncool. Mais vous pouvez facilement le faire de cette façon, si vous le voulez vraiment. Déplacez votre CHANGE_TOTAL = (float(input('type in the change total ')))l'intérieurmain et mettre une directive 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) 

ici serait une conception plus sensible, à mon avis:

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) 

Remarque, j'utilise un certain temps en boucle au lieu d'un commun récursion.

+0

merci ça marche, je vais repenser la façon dont j'écris le code de toute façon, j'ai commencé à apprendre python il y a deux semaines avec un appel pdf commençant par python c'est un très bon livre loin –

0

J'ai modifié un peu votre code. C'est une bonne pratique dans la programmation de ne pas définir les variables globales, ils vont changer car il devient presque impossible dans les grandes entreprises de suivre les erreurs. Il est préférable de transmettre la valeur à modifier dans une fonction, puis de renvoyer sa nouvelle valeur. Cela rend beaucoup de base de code plus facile de tester des fonctions individuelles pour les erreurs. Si vous êtes désireux de retourner plusieurs valeurs, il suffit de retourner tuples et déballer, par exemple

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 

J'ai également ajouté un essai, sauf le bloc qui fera le programme reste ouvert si vous mal tapé par mégarde une valeur, ce qui est vraiment bon entraine toi.

#!/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()