1
def sumOfStudentDigits():  
    studentdigit = (studentdigit1 + studentdigit2 + studentdigit3 + studentdigit4 + studentdigit5 + studentdigit6 + studentdigit7)

    studentdigit1=3 studentdigit2=6 studentdigit3=9 studentdigit4=3 
           studentdigit5=1 studentdigit6=0 studentdigit7=0

I need to assign seven digits to seven variables and add them together.

lxndev
  • 7
  • 2
j. bell
  • 21
  • 5
  • Can you clarify what the question is? What type of function are you attempting to build? – Kyle Sep 17 '17 at 04:11
  • i am a student doing an assignment and i have to make this code for the progam to recongnise who i am when i open it – j. bell Sep 17 '17 at 04:12
  • when ever i try to run the program it comes up with a syntax error and i dont know what im doing wrong – j. bell Sep 17 '17 at 04:16
  • All of your "studentdigit[1-7]" variables are not defined correctly. Is the function supposed to add the digits together? – Kyle Sep 17 '17 at 04:19
  • yeah i had previously had the varible and it was still saying that there was a syntax error with the first line of code – j. bell Sep 17 '17 at 04:20
  • i added the variables and now i am getting this error: The error value is: unindent does not match any outer indentation level (assignment1 (2).jy, line 46) Improper indentation. A line of code contains bad indentation. Make sure all of your lines match up inside your functions. – j. bell Sep 17 '17 at 04:25
  • Can you edit your post with your new code? I can help you through it. – Kyle Sep 17 '17 at 04:29
  • The error says it all. Your code isn't properly indented. Check the indentation at the line you are getting this error. – Ank Sep 17 '17 at 04:34
  • Okay thanks! It looks like you are trying to reference variables before they are being created which will cause errors. You also need to make sure that your indentations are consistent. Remember, white-space is important when writing python. I would avoid having a variable for each digit in the student number. You can just treat it as a full number and go from there. – Kyle Sep 17 '17 at 04:37
  • i have no idea what you just said – j. bell Sep 17 '17 at 04:38
  • I would create a function that takes a student number as a parameter and returns the sum of all of the digits. The part that you need to figure out is how to break that student number that is getting passed into the function as a parameter into digits so you can attempt to add them together. – Kyle Sep 17 '17 at 04:38

3 Answers3

1

If your confusion is how to get the studentdigits into your function, you can pass them into the function like this:

def sumOfStudentDigits(studentdigit1, studentdigit2, studentdigit3,
                       studentdigit4, studentdigit5, studentdigit6,
                       studentdigit7):
    studentdigit = (studentdigit1
                    + studentdigit2
                    + studentdigit3
                    + studentdigit4
                    + studentdigit5
                    + studentdigit6
                    + studentdigit7)

My advice would be to have all those digits stored in a list, and then pass merely that list to the function, then iterate over the list:

listofdigits = [studentdigit1,
                studentdigit2,
                studentdigit3,
                studentdigit4,
                studentdigit5,
                studentdigit6,
                studentdigit7]

def sumOfStudentDigits(studentdigitlist):
    sum = 0
    for digit in studentdigitlist:
        sum += digit
        return sum

print(sumOfStudentDigits(listofdigits))

We have to set sum = 0 before we can use sum because python wants to know what sum is before it uses it, so we assign it 0 so that we can count up from there. Notice how studentdigitlist and listofdigits are different? You can pass a list of any name to the function, all that matters is that you use the variable (ie a list in this case) name that you have used in def myfunction(yourvariable): throughout the function definition. Python with substitute whatever you pass into the function for where you have that placeholder name within the function. Then when you run the function: eg

def myfunction(yourvariable):
    # do stuff with yourvariable
    myvariable = myvariable + 7

somenumber = 2
myfunction(somenumber)
# now somenumber will equal 9
toonarmycaptain
  • 2,093
  • 2
  • 18
  • 29
  • ive input this code but i dont understand how to divide the code example how greenfoot uses brackets to split up different code. because its still coming up with the syntax error – j. bell Sep 17 '17 at 04:46
  • What is `greenfoot`? What is the line you are being told the syntax error is in? – toonarmycaptain Sep 17 '17 at 04:50
  • they first line is saying that i have a syntax error – j. bell Sep 17 '17 at 04:50
  • what is the first line? – toonarmycaptain Sep 17 '17 at 04:52
  • the list of digits – j. bell Sep 17 '17 at 04:52
  • I mean paste it in here. If you've an errant `.`, `&`or a `)` missing, it can throw an error, but we can't help you very well if we don't know what you're seeing on your screen. – toonarmycaptain Sep 17 '17 at 04:55
  • listofdigits = [studentdigit1,studentdigit2,studentdigit3,studentdigit4,studentdigit5,studentdigit6,studentdigit7] def sumOfStudentDigits(studentdigitlist): sum = 0 for digit in studentdigitlist: sum += digit return sum print(sumOfStudentDigits(listofdigits)) – j. bell Sep 17 '17 at 04:56
  • You need spaces after each comma in the list assignment. Lists in python are separated by commas and spaces eg `list = [1, 2, 3, 4, 5]` – toonarmycaptain Sep 17 '17 at 04:57
  • ive added the spaces but the error is saying it is an ivalid syntax – j. bell Sep 17 '17 at 04:59
  • if your first line is `list = [a, b, c, d, e]` it will throw an error because you don't have any values assigned to variable a, b, c, d, or e - you're referencing variables before they have been defined. You can make your list up of the variables directly `list = [1, 2, 3, 4, 5]` or you assign values to your `studentdigit` variables eg a = 1 b = 2 (these have to be on separate lines), before putting them in a list. – toonarmycaptain Sep 17 '17 at 05:06
  • okay all of the code has cleared but the error now is saying there are no closing parentheses. – j. bell Sep 17 '17 at 05:17
  • Well find where you've opened a parenthesis and not closed it? every ( or [ needs a ), ]. Also if I answered your original question, please mark it as such :) – toonarmycaptain Sep 17 '17 at 05:18
  • found it i had a random ] somewhere for some reason – j. bell Sep 17 '17 at 05:21
0

You could also pass in the entire student number and break it down inside of your function.

def sum_student_digits(student_id):
    running_total = 0

    for i in str(student_id):
        running_total += int(i)

    return running_total

print(sum_student_digits(12345))
Kyle
  • 1,056
  • 5
  • 15
0

Keeping things basic. You need to assign the seven digit student number, one to each variable.

def sumOfStudentDigits():  
    digit1 = 3
    digit2 = 6
    digit3 = 9
    digit4 = 3
    digit5 = 1
    digit6 = 0
    digit7 = 0

And then add them together:

    print(digit1 + digit2 + digit3 + digit4 + digit5 + digit6 + digit7)

Note that the variable assignments can't be on the same line, and need to come before the sum.

lxndev
  • 7
  • 2