Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › 'x' not define help in Python from a beginner
New Posts  All Forums:Forum Nav:

'x' not define help in Python from a beginner

post #1 of 2
Thread Starter 
Hi all, i've begun Python's class in university and we had to write a small program that can do simple math things but that's not the problem. I have written the program which (seem!) to work well and we can correct our program using program on the university's website, but only 5 times, so I cant use it all the time Here the program (in french):

import argparse

parser = argparse.ArgumentParser(description='TP1: traiter une série de nombres')
parser.add_argument('nombres', type=float, nargs='+', metavar='nombre',
help='Entrer une série de nombres séparés par des espaces')
parser.add_argument('--op', choices=,
default='somme', help="Opération à effectuer (défaut=somme)")
args = parser.parse_args()

# écrire votre programme ci-dessous;
# placer la réponse dans la variable «resultat»;
# «args.op» contient le nom de l'opération
# «args.nombres» contient la liste des nombres
if args.op == 'somme':
somme = 0
for nombre in args.nombres:
somme += nombre
resultat = somme
elif args.op == "spair":
spair = 0
for nombre in args.nombres[::2]:
spair += nombre
resultat = spair
elif args.op == "simpair":
simpair = 0
for nombre in args.nombres[1::2]:
simpair += nombre
resultat = simpair
elif args.op == "scarre":
scarre = 0
for nombre in args.nombres:
scarre += nombre**2
resultat = scarre
# affichage du résultat à la console
print(args.op, '=', resultat)

The first part was already given by the teacher, i had to write all the ''if'' and the error that the program give me (and does not in eclipse) is
Fichier /tp1.py", line 49, in print(args.op, '=', resultat) NameError: name 'resultat' is not defined

I supposed i have to define 'resultat' but how exactly? thanks! smile.gif
My Rig
(18 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500k oc 4.2ghz  Gigabyte Z68A-D3H-B3 Gigabyte Windforce HD 7950 8gb 1600mhz G.Skill 
Hard DriveHard DriveOptical DriveCooling
Samsung F3 spinpoint OCZ Vertex 3 60 gb Asus Corsair H100 
OSMonitorMonitorKeyboard
Window 7 64 bits pro Dell u2211h 1080p Qnix 2560 x 1440 @96hz Black widow 
PowerCaseMouseMouse Pad
Antec EA 750w Corsair 500r black razer imperator Steelseries 
AudioAudio
Beyerdynamic DT 770 Pro Asus xonar dg 
  hide details  
Reply
My Rig
(18 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500k oc 4.2ghz  Gigabyte Z68A-D3H-B3 Gigabyte Windforce HD 7950 8gb 1600mhz G.Skill 
Hard DriveHard DriveOptical DriveCooling
Samsung F3 spinpoint OCZ Vertex 3 60 gb Asus Corsair H100 
OSMonitorMonitorKeyboard
Window 7 64 bits pro Dell u2211h 1080p Qnix 2560 x 1440 @96hz Black widow 
PowerCaseMouseMouse Pad
Antec EA 750w Corsair 500r black razer imperator Steelseries 
AudioAudio
Beyerdynamic DT 770 Pro Asus xonar dg 
  hide details  
Reply
post #2 of 2
Thread Starter 
Nevermind
I forgot to add in the first place resultat = 0 ! everything worked and i've got 10/10!
My Rig
(18 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500k oc 4.2ghz  Gigabyte Z68A-D3H-B3 Gigabyte Windforce HD 7950 8gb 1600mhz G.Skill 
Hard DriveHard DriveOptical DriveCooling
Samsung F3 spinpoint OCZ Vertex 3 60 gb Asus Corsair H100 
OSMonitorMonitorKeyboard
Window 7 64 bits pro Dell u2211h 1080p Qnix 2560 x 1440 @96hz Black widow 
PowerCaseMouseMouse Pad
Antec EA 750w Corsair 500r black razer imperator Steelseries 
AudioAudio
Beyerdynamic DT 770 Pro Asus xonar dg 
  hide details  
Reply
My Rig
(18 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500k oc 4.2ghz  Gigabyte Z68A-D3H-B3 Gigabyte Windforce HD 7950 8gb 1600mhz G.Skill 
Hard DriveHard DriveOptical DriveCooling
Samsung F3 spinpoint OCZ Vertex 3 60 gb Asus Corsair H100 
OSMonitorMonitorKeyboard
Window 7 64 bits pro Dell u2211h 1080p Qnix 2560 x 1440 @96hz Black widow 
PowerCaseMouseMouse Pad
Antec EA 750w Corsair 500r black razer imperator Steelseries 
AudioAudio
Beyerdynamic DT 770 Pro Asus xonar dg 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › 'x' not define help in Python from a beginner