dolphins123
Badges: 10
Rep:
?
#1
Report Thread starter 1 month ago
#1
what is wrong with this code?

Nums="24,26,45,51,90"
Numslist=Nums.split(",")
Total=0
for i in range(1,len(Numslist)):
Total=Total+(int(Numslist[i])-int(Numslist[i-1]))
print(int(Numslist[i],int(Numslist[i-1]),int(Numslist[i])-int(Numslist[i-1]))
print(Total)

thank you
0
reply
ParadoxSocks
Badges: 22
Rep:
?
#2
Report 1 month ago
#2
What do you think the code should be doing and what problem do you encounter when you run it?
0
reply
dolphins123
Badges: 10
Rep:
?
#3
Report Thread starter 1 month ago
#3
(Original post by ParadoxSocks)
What do you think the code should be doing and what problem do you encounter when you run it?
the code should output the difference between two numbers from the list of integers in 'nums'. however when i put this in python, it says invalid syntax and highlights the final 'print' of the code.
0
reply
ParadoxSocks
Badges: 22
Rep:
?
#4
Report 1 month ago
#4
(Original post by dolphins123)
the code should output the difference between two numbers from the list of integers in 'nums'. however when i put this in python, it says invalid syntax and highlights the final 'print' of the code.
Have you matched up your brackets to make sure all opened brackets are closed?

Is Total working as you expect it to before then?
0
reply
dolphins123
Badges: 10
Rep:
?
#5
Report Thread starter 1 month ago
#5
(Original post by ParadoxSocks)
Have you matched up your brackets to make sure all opened brackets are closed?

Is Total working as you expect it to before then?
i matched the brackets up, and the total seems to be working before the last calculation occurs
0
reply
ParadoxSocks
Badges: 22
Rep:
?
#6
Report 1 month ago
#6
(Original post by dolphins123)
i matched the brackets up, and the total seems to be working before the last calculation occurs
Try printing the contents of Numslist[i] within your loop to see if the numbers you're accessing are correct.
0
reply
ParadoxSocks
Badges: 22
Rep:
?
#7
Report 1 month ago
#7
Or maybe try printing these one at a time just to double check your brackets are all where they should be

print(int(Numslist[i], int(Numslist[i-1]), int(Numslist[i]) - int(Numslist[i-1]))
0
reply
winterscoming
Badges: 19
Rep:
?
#8
Report 1 month ago
#8
(Original post by dolphins123)
i matched the brackets up, and the total seems to be working before the last calculation occurs
Consider what happens at each iteration.

The initial value of Total is zero.

After Iteration 1: Total = 0 + 26 - 24 = 2
value of Total after iteration 1 = 2

After Iteration 2: Total = 2 + 45 - 26 = 21
value of Total after iteration 2 = 21

After Iteration 3: Total = 21 + 51 - 45 = 27
value of Total after iteration 3 = 27

After Iteration 4: Total = 27 + 90 - 51 = 66
value of Total after iteration 4 = 66

Are you sure this calculation is what you'd intended?



Also, consider simplifying your code by doing the int conversions separately. That should make your life a bit easier, so you can see what's happening, hopefully leading to fewer mistakes and less time staring at it to figure out what you might have done wrong - for example, adding these two lines in will make your 'Total' calculation really short and simple:
Code:
for i in range(1,len(Numslist)):
    first = int(Numslist[i-1])
    second = int(Numslist[i])
    Total = Total + second - first
Remember, for your own benefit (and for your teacher, the examiner, and anyone else who might be looking at your code to help you) that it's usually bad to have individual lines of busy/complicated code doing lots of things because it means you or whoever else is trying to read it has got more stuff to think about. (having all those brackets on one line is a bit much to read, right?)

You're almost always much better off writing more 'verbose' code which looks simple and easy for a human to understand! - so splitting a program into a greater number of smaller, simpler steps is often a good thing
Last edited by winterscoming; 1 month ago
1
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Regarding Ofqual's most recent update, do you think you will be given a fair grade this summer?

Yes (256)
33.86%
No (500)
66.14%

Watched Threads

View All
Latest
My Feed