0

How do i add this? Please help

def module(number): mod = int(raw_input('Module'+str(number)+'grade achieved: ')) if mod >=80 and mod <=100: print ('Distinction') elif mod >=60 and mod <=79: print ('Merit') elif mod >=40 and mod<=59: print ('Pass') else: print ('You have not passed this Module') for num in range (6): module(num+1)

13th Jan 2018, 9:23 PM
Sponge Bob
Sponge Bob - avatar
34 Antworten
+ 3
# For your last question, this is because you are testing if lower or equal... it will work if you test for equality: Distinction = 20 Merit = 15 Pass = 10 def caopoints(number): cao = input('Module'+str(number)+'grade achieved: ') # or #cao = int(raw_input('Module'+str(number)+'grade achieved: ') if cao== Distinction: print('20 points') # or: print(str(20)+' points'), but longer, and not necessary elif cao== Merit: print('15 points') # also here elif cao== Pass: print('10 points') # and here else: print ('You have not passed this Module') return cao sum=0 for num in range (1,7): sum +=caopoints(num) levelsix = raw_input("Is the course level 6? (y/n)") if levelsix.lower()=="y": sum*=1.25 print (sum)
14th Jan 2018, 3:05 AM
visph
visph - avatar
+ 2
You need to return the value from within the function, and sum then in your loop: def module(number): # ... return mod sum = 0 for num in range(6): sum += module(num+1) Anyway, raw_input() is specific to Python less than 3... since Python 3, only input() is available, ans act as raw_input() in 2.x. Also, in Python 2.x, if you use input(), you don't need to cast the returned value to int, as this is done silently... Finally, your loop could be a little improved as this: for num in range(1,7): sum += module(num)
13th Jan 2018, 10:02 PM
visph
visph - avatar
+ 2
"# ..." is just to shorten my post and mean that you put here your actual function body ^^
13th Jan 2018, 10:09 PM
visph
visph - avatar
+ 2
# Sure, after the for loop, you need to print the sum ^^ for num in range(1,7): sum += module(num) print(sum)
13th Jan 2018, 10:16 PM
visph
visph - avatar
+ 2
Assuming Distinction, Merit and Pass are previously defined with number, don't you also need to compute the sum? And what about the 1.25 multiplication if level 6? Anyway, what's your question? To do all that things, just change the module() function by the new caopoints() one, in the code we have established in your other thread ^^
14th Jan 2018, 1:35 AM
visph
visph - avatar
+ 2
# I think (I'm not sure to good understand) you just need to declare and initialize the 3 variables at begin of your code: Distinction = 20 Merit = 15 Pass = 10
14th Jan 2018, 1:58 AM
visph
visph - avatar
+ 2
""" anyway, in your last posts, you will print(sum) at end only if the condition is true, and in addition, you doesn't test the right variable: 'inp' was the name I give to receive the user input, but in your code you store it in 'levelsix' variable, so you need: """ btw I have not put this on my original code because I wanted to know what is wrong with it. This is on a different file. and also I changed it but it still gave me the same error. levelsix = raw_input("Is the course level 6? (y/n)") if levelsix.lower()=="y": sum*=1.25 print (sum) # if you keep indentation, print is executed only if condition is true
14th Jan 2018, 2:03 AM
visph
visph - avatar
+ 2
Sorry, I just see the mistake now: you must use input() instead raw_input(), or use int(raw_input()) (raw_input return a string, while input() try to convert it to the well suited type (int or float in case of number entered)... All that is Python2.7 specific ^^
14th Jan 2018, 2:19 AM
visph
visph - avatar
+ 2
# this would work: Distinction = 20 Merit = 15 Pass = 10 def caopoints(number): cao = input('Module'+str(number)+'grade achieved: ') # or #cao = int(raw_input('Module'+str(number)+'grade achieved: ') if cao<= Distinction: print('20 points') # or: print(str(20)+' points'), but longer, and not necessary elif cao<= Merit: print('15 points') # also here elif cao<= Pass: print('10 points') # and here else: print ('You have not passed this Module') return cao sum=0 for num in range (1,7): sum +=caopoints(num) levelsix = raw_input("Is the course level 6? (y/n)") if levelsix.lower()=="y": sum*=1.25 print (sum) # are you sure you want to print sum only if level 6 ?
14th Jan 2018, 2:38 AM
visph
visph - avatar
+ 2
So retrieve the spaces at begin of line print(sum) ^^
14th Jan 2018, 2:53 AM
visph
visph - avatar
+ 2
""" Did you enter number at input or word? If the goal is to input word, the 'int(raw_input())' solution will not work ;) The way input() works in Python before 3, is that if you enter word, Python interpreter will search for a variable of same name and return the value stored in it... So, if you input 'distinction' rather than 'Distinction' you will get an undefined name error... Another way to do would be: """ grades = { "distinction" : 20, "merit" : 15, "pass" : 10 } def caopoints(number): cao = raw_input('Module '+str(number)+' grade achieved: ') cao = cao.lower() if cao in grades: print(str(grades[cao]+' points') else: print ('You have not passed this Module or you didn't enter a valid grade') return grade[cao] sum=0 for num in range (1,7): sum +=caopoints(num) levelsix = raw_input("Is the course level 6? (y/n)") if levelsix.lower()=="y": sum*=1.25 print (sum)
14th Jan 2018, 3:03 AM
visph
visph - avatar
0
thank you, I have but I am haivng trouble adding them at the end. I can't put it as total= module(number+1)+module(number+1).. etc
13th Jan 2018, 9:31 PM
Sponge Bob
Sponge Bob - avatar
0
Hi! Thank you for noticing my question. Im glad you answered it. Im a bit confused with def module:(number): #... return mod
13th Jan 2018, 10:08 PM
Sponge Bob
Sponge Bob - avatar
0
hi, when I tried the code, It doesnt show the total sum. could it be that Im using 2.7?
13th Jan 2018, 10:15 PM
Sponge Bob
Sponge Bob - avatar
0
Ohhhhhhh, Thats why ahahaha.. Im so blind. Sorry for wasting your time Mr.visph. You're a wizard.
13th Jan 2018, 10:21 PM
Sponge Bob
Sponge Bob - avatar
0
visph, I gave my assignment and my teacher said I mixed my code up. this code ahhaahahaha def module(number): mod = int(input('Module'+str(number)+'grade achieved: ')) if mod >=80 and mod <=100: print ('Distinction') elif mod >=60 and mod <=79: print ('Merit') elif mod >=40 and mod<=59: print ('Pass') else: print ('You have not passed this Module') return mod
14th Jan 2018, 1:24 AM
Sponge Bob
Sponge Bob - avatar
0
apparently its suppose to be like this def caopoints(number): cao = int(raw_input('Module'+str(number)+'grade achieved: ')) if cao<= Distinction: print ('20 points achieved') elif cao<= Merit: print ('15 points achieved') elif cao<= Pass: print ('10 points achieved') else: print ('You have not passed this Module') return cao for num in range (1,7): caopoints(num)
14th Jan 2018, 1:25 AM
Sponge Bob
Sponge Bob - avatar
0
Im so stupid. I tried changing it but for some reason it doesnt work anymore
14th Jan 2018, 1:27 AM
Sponge Bob
Sponge Bob - avatar
0
My teacher didnt have a problem with computing the *1.25 if lvl6 and adding them to find the total value. She just said that I made a mistake where Im suppose to be asking for (Distinction, Merit, Pass) and equal them to where Distinction=20, Merit=15 and Pass=10. but when I tried changing the original code to this , It doesnt work anymore. def caopoints(number): cao = int(raw_input('Module'+str(number)+'grade achieved: ')) if cao<= Distinction: print ('20 points achieved') elif cao<= Merit: print ('15 points achieved') elif cao<= Pass: print ('10 points achieved') else: print ('You have not passed this Module') return cao sum=0 for num in range (1,7): sum +=caopoints(num) levelsix = raw_input("Is the course level 6? (y/n)") if inp.lower()=="y": sum*=1.25 print (sum)
14th Jan 2018, 1:44 AM
Sponge Bob
Sponge Bob - avatar
0
My question is, How do I get def caopoints(number): cao = int(raw_input('Module'+str(number)+'grade achieved: ')) if cao<= Distinction: print ('20 points achieved') elif cao<= Merit: print ('15 points achieved') elif cao<= Pass: print ('10 points achieved') else: print ('You have not passed this Module') return cao sum=0 for num in range (1,7): sum +=caopoints(num) levelsix = raw_input("Is the course level 6? (y/n)") if inp.lower()=="y": sum*=1.25 print (sum) without this error: Traceback (most recent call last): File "C:\Users\Pixelated\Documents\stuff1.py", line 15, in <module> sum +=caopoints(num) File "C:\Users\Pixelated\Documents\stuff1.py", line 2, in caopoints cao = int(raw_input('Module'+str(number)+'grade achieved: ')) ValueError: invalid literal for int() with base 10: 'Distinction'
14th Jan 2018, 1:48 AM
Sponge Bob
Sponge Bob - avatar