0

I'm working with python 2.7 and basically my python script consist of opening a csv file, looping into the rows to find the one with the id the user entered. Once the row is found, depending on some conditions the script upadtes three colunms with others values the user entered.

Here is my code:

# -*-coding:Latin-1 -*

import csv
from csv import reader
from csv import DictReader

print("")
print("Sélectionez une operation.")
print("")
print("1. Mettre à jour les indicateurs des tables ENTREPRISE")
print("")
print("2. Mettre à jour les indicateurs des tables PARTICULIER")
print("")
# Nos fichiers csv
# PMGE
Bfr = csv.reader(open('/home/cloudera/PMGE/Bfr.csv'))
linesBfr = list(Bfr)
CredBailImmo = csv.reader(open('/home/cloudera/PMGE/CredBailImmo.csv​'))
linesCredBailImmo = list(CredBailImmo)

def scoreBas(x,y):
    return 6*0.5 + x*0.3 + y*0.2

def scoreMoyen(x,y):
    return 4*0.5 + x*0.3 + y*0.2

def scoreEleve(x,y):
    return 2*0.5 + x*0.3 + y*0.2

while True:
    # Récupérer une entrée de l'utilisateur
    choix = input("Entrez votre choix (1/2): ")

    # Vérifions si le choix fait partie de nos options
    if choix in (1, 2): 
        if choix == 1:
        print("")
        print("CHoisissez la table à modifier.")
        print("")
        print("     1. Besoin en Fonds de Roulement")
        print("     2. Credit bail Immobilier")
        print("")
        while True:
                choix = input("Entrez votre choix(1/ 2/: ")
            if choix in (1, 2): 
                    if choix == 1:
                    print("")
                    num1 = str(input("Entez le numéro de l'entreprise à modifier: "))
                    num2 = int(input("Entez la nouvelle valeur de l'indicateur de confiance: "))
                    num3 = int(input("Entez la nouvelle valeur de l'indicateur de persévérance: "))
                    i = 0
                    while linesBfr[i][0] != num1:
                        i +=1
                        #indx conf = 32; pers = 33; score = 34;
                        if linesBfr[i][0] == num1:
                            #affichage les valeurs actuelle
                            print("")
                            print("Ancienne valeur de l'indicateur de confiance:")
                            print(linesBfr[i][32])
                            print("Ancienne valeur de l'indicateur de persévérance:")
                            print(linesBfr[i][33])
                            print("Ancienne valeur du score:")
                            print(linesBfr[i][34])
                            #on modifie les indincateurs dans la liste
                            linesBfr[i][32] = num2
                            linesBfr[i][33] = num3
                            #on écris les modifications dans le fichier csv
                            writer = csv.writer(open('/home/cloudera/PMGE/Bfr.csv', 'w'))
                            writer.writerows(linesBfr)
                            #on modifie le score
                            if linesBfr[i][31] == 'Bas':
                                linesBfr[i][34] = scoreBas(int(linesBfr[i][32]),int(linesBfr[i][33]))
                                writer = csv.writer(open('/home/cloudera/PMGE/Bfr.csv', 'w'))
                                writer.writerows(linesBfr)
                            elif linesBfr[i][31] == 'Moyen':
                                linesBfr[i][34] = scoreMoyen(int(linesBfr[i][32]),int(linesBfr[i][33]))
                                writer = csv.writer(open('/home/cloudera/PMGE/Bfr.csv', 'w'))
                                writer.writerows(linesBfr)
                            else:
                                linesBfr[i][34] = scoreEleve(int(linesBfr[i][32]),int(linesBfr[i][33]))
                                writer = csv.writer(open('/home/cloudera/PMGE/Bfr.csv', 'w'))
                                writer.writerows(linesBfr)
                            #affichage des nouvelles valeurs
                            print(linesBfr[i][0])
                            print(linesBfr[i][31])
                            print("")
                            print("Nouvelle valeur de l'indicateur de confiance:")
                            print(linesBfr[i][32])
                            print("Nouvelle valeur de l'indicateur de persévérance:")
                            print(linesBfr[i][33])
                            print("Nouvelle valeur du score:")
                            print(linesBfr[i][34])
                elif choix == 2:
                    for row in CredBailImmo :
                        print(row)
                break
                else:
                    print("Invalid Input")
        elif choix == 2:
            print("Choisissez la table à modifier.")
        print("1. ")
        print("2. ")
        break
    else:
        print("Invalid Input")

But when i execute the script the third value linesBfr[i][34] is not updated. I tried changing the conditions respectively by (converting to strings):

if str(linesBfr[i][31]) == 'Bas':
    linesBfr[i][34] = scoreBas(int(linesBfr[i][32]),int(linesBfr[i][33]))
    writer = csv.writer(open('/home/cloudera/PMGE/Bfr.csv', 'w'))
    writer.writerows(linesBfr)
elif str(linesBfr[i][31]) == 'Moyen':
    linesBfr[i][34] = scoreMoyen(int(linesBfr[i][32]),int(linesBfr[i][33]))
    writer = csv.writer(open('/home/cloudera/PMGE/Bfr.csv', 'w'))
    writer.writerows(linesBfr)
else:
    linesBfr[i][34] = scoreEleve(int(linesBfr[i][32]),int(linesBfr[i][33]))
    writer = csv.writer(open('/home/cloudera/PMGE/Bfr.csv', 'w'))
    writer.writerows(linesBfr)

But still nothing. Can anyone help me figure out what i'm doing wrong?

emeric
  • 59
  • 2
  • 11
  • You should create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) rather than posting your entire program. This should include anonymous example of the input csv files. Try to get one option working first so that you understand it and know it works, then expand to all the options you want. – Alan Jul 11 '21 at 13:53
  • Are you asking why your conditional statement is not working? Have you determined that none of the three suites are executing? Or are you asking why the values haven't changed when you subsequently look at the fle? – wwii Jul 11 '21 at 14:00
  • If you are using an IDE **now** is a good time to learn its debugging features Or the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – wwii Jul 11 '21 at 14:15
  • @wwii I have applied your suggestion and i figure out that it only execute the last part of the conditional expression the "else" part, even if the the first or second conditions are satisfied. – emeric Jul 11 '21 at 17:21
  • Looks like there is an indentation problem at/after: `if choix == 1: print(""); num1 = str(input("Entez ...` - please fix. – wwii Jul 12 '21 at 18:34
  • I see you are printing `linesBfr[i][31]` - have you looked at those? Is there anything *funny* about them like capitalization or spelling or extra spaces? Do they all match the strings you are testing for? – wwii Jul 12 '21 at 18:36
  • @wwii yes when i'm printing ``` linesBfr[i][31] ``` to verify which value i'm going to pass in the conditions. If the value is "Bas" it executes the code under the first "if" and if the value is different it still executes the code under first "if". When i'm switching the formulation of the conditions from ``` == ``` to ``` != ``` regardless of the value of ``` linesBfr[i][31] ```, it executes the code under "else" – emeric Jul 13 '21 at 18:05
  • @wwii I resolved my situation. The problem was not coming from my python code but from my csv file. There were spaces in the element that I passed in the if else statements. Thanks for you suggestions. – emeric Jul 14 '21 at 11:59

1 Answers1

0

This is NOT an answer - if you simplify the offending conditional expression, it may make it easier for you to troubleshoot.

name = str(linesBfr[i][31])
if name == 'Bas':
    placeholder = scoreBas
elif name == 'Moyen':
    placeholder = scoreMoyen
else:
    placeholder = scoreEleve
linesBfr[i][34] = placeholder(int(linesBfr[i][32]),int(linesBfr[i][33]))

with open('/home/cloudera/PMGE/Bfr.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerows(linesBfr)
wwii
  • 23,232
  • 7
  • 37
  • 77
  • I have applied your suggestion and i figure out that it only execute the last part of the conditional expression the "else" part, even if the the first or second conditions are satisfied. – emeric Jul 11 '21 at 17:20
  • And when i change the conditional expression from "if condition == a" to "if condition != b and conditionc!= c" it only execute the code under the first "if" and ignore the second "if" and the "else" even if the condition is not satisfied. – emeric Jul 11 '21 at 19:54