The Student Room Group

Basic Python help please

I've just started a Computing course. Can't work out why this programme won't work properly.

#Average calculator

NumberOne=int(input('First number?'))
NumberTwo=int(input('Second number?'))
NumberThree=int(input('Third number?'))
average=(NumberOne+NumberTwo+NumberThree)/3
sum=(NumberOne+NumberTwo+NumberThree)/3
print ('The average of the 3 numbers is",average,"')

It lets me input the numbers but doesn't display the actual average. Just says, The average of the 3 numbers is",average,"
Reply 1
Original post by JoeUtd
I've just started a Computing course. Can't work out why this programme won't work properly.

#Average calculator

NumberOne=int(input('First number?'))
NumberTwo=int(input('Second number?'))
NumberThree=int(input('Third number?'))
average=(NumberOne+NumberTwo+NumberThree)/3
sum=(NumberOne+NumberTwo+NumberThree)/3
print ('The average of the 3 numbers is",average,"')

It lets me input the numbers but doesn't display the actual average. Just says, The average of the 3 numbers is",average,"


Why so many quotes?

Why not:

print ('The average of the 3 numbers is', average)

I've never actually coded in Python though so it could be completely wrong.
EDIT: Nevermind, I should learn to read. And realize how old some threads are.
(edited 12 years ago)
Reply 3
Original post by JoeUtd
I've just started a Computing course. Can't work out why this programme won't work properly.

#Average calculator

NumberOne=int(input('First number?'))
NumberTwo=int(input('Second number?'))
NumberThree=int(input('Third number?'))
average=(NumberOne+NumberTwo+NumberThree)/3
sum=(NumberOne+NumberTwo+NumberThree)/3
print ('The average of the 3 numbers is",average,"')

It lets me input the numbers but doesn't display the actual average. Just says, The average of the 3 numbers is",average,"


At a quick glance, I think the last line should be:
print('The average of the 3 numbers is', average)

Also not quite sure what sum = (NumberOne+NumberTwo+NumberThree)/3 is doing there, tbh

EDIT: Just realised how old this thread is :lol:
(edited 12 years ago)
Reply 4
Original post by JoeUtd
I've just started a Computing course. Can't work out why this programme won't work properly.

#Average calculator

NumberOne=int(input('First number?'))
NumberTwo=int(input('Second number?'))
NumberThree=int(input('Third number?'))
average=(NumberOne+NumberTwo+NumberThree)/3
sum=(NumberOne+NumberTwo+NumberThree)/3
print ('The average of the 3 numbers is",average,"')

It lets me input the numbers but doesn't display the actual average. Just says, The average of the 3 numbers is",average,"


I've also never coded in Python, however I agree that the quotation marks are the problem. In the print() line, you open a string with a single quotation mark, so I would presume you have to close your string with a single quotation mark as well, and THEN add on (or concatenate, with the comma) the number stored as "average".

As the guy up there said, I'd guess: print ('The average of the 3 numbers is',average) would work.
(edited 12 years ago)
Reply 5
Original post by JoeUtd
I've just started a Computing course. Can't work out why this programme won't work properly.

#Average calculator

NumberOne=int(input('First number?'))
NumberTwo=int(input('Second number?'))
NumberThree=int(input('Third number?'))
average=(NumberOne+NumberTwo+NumberThree)/3
sum=(NumberOne+NumberTwo+NumberThree)/3
print ('The average of the 3 numbers is",average,"')

It lets me input the numbers but doesn't display the actual average. Just says, The average of the 3 numbers is",average,"


Something along the lines of

print("The average of the 3 numbers is "+average)

should do the trick, + works with variable names as it's easily noticeable at a glance what it's doing, if you throw some " and ' and then , in the mix, it can all get confusing. I've just started A-Level Computer Science, and i'm used to C++ and other things so i tend to throw brackets everywhere, but remember that commenting and spacing is your friend, this will help you in the exam.

Also another thing is that " and ' are the same, but make sure you only use one of them constantly, if an examiner looks at your work and there's sometimes " used in prints and then ' in other times, you'll get marked down. You might be better off sticking with " as it's alot easier to see, and some editors will highlight it nicely for you. Just remember that only put quotes around a string you want printing, the variable doesn't actually need to be followed by "" at all, just close the brackets for the print function and you should be fine.

Also your sum has the variable NumberThree incorrect, you've put it as NumberT hree.

Hope this all helps, also i'm new here, so any thanks are appreciated
(edited 12 years ago)

Quick Reply

Latest

Trending

Trending