Practice python task for exam?











up vote
0
down vote

favorite












I'm practicing previous python exam question for an upcoming exam but my code is screwy.




  1. The task is to make a text file with a list of weights in grams (done).


  2. Prompts user for file name, reads the weights, adds them in a list and calculates the total weight.



Herein lies the problem:



try:

file = input('Enter file name:')

f = open('weights.txt', 'r')

sum=0

for line in f:

sum = sum+(int(line.strip()))/1000

print('The textbook weight in kg:', sum)

except:

print('File cannot be opened')


The output the programme shows is:



"The textbook weight in kg: 0.5



The textbook weight in kg: 0.65



The textbook weight in kg: 1.35



The textbook weight in kg: 1.6500000000000001



The textbook weight in kg: 1.9000000000000001"



But the output i need is:



1.9 only,without the previous lines.



As I'm still a beginner, I know very little about the correct code. So any help will be appreciated










share|improve this question
























  • Can you elaborate on what exactly is the issue? Or the expected output?
    – dmitriys
    Nov 13 at 0:21










  • The expected output is 1.9kg
    – topu
    Nov 13 at 0:22










  • This is just floating point inaccuracy, you just need to round the numbers to something more appropriate (such as to two decimal places). You can do this by round(output, 2)
    – Polymer
    Nov 13 at 0:25










  • For clarification, floating point inaccuracy is explained here effbot.org/pyfaq/…
    – Polymer
    Nov 13 at 0:26










  • There is this very non beginner friendly answer stackoverflow.com/questions/588004/…
    – Paul Rooney
    Nov 13 at 0:27















up vote
0
down vote

favorite












I'm practicing previous python exam question for an upcoming exam but my code is screwy.




  1. The task is to make a text file with a list of weights in grams (done).


  2. Prompts user for file name, reads the weights, adds them in a list and calculates the total weight.



Herein lies the problem:



try:

file = input('Enter file name:')

f = open('weights.txt', 'r')

sum=0

for line in f:

sum = sum+(int(line.strip()))/1000

print('The textbook weight in kg:', sum)

except:

print('File cannot be opened')


The output the programme shows is:



"The textbook weight in kg: 0.5



The textbook weight in kg: 0.65



The textbook weight in kg: 1.35



The textbook weight in kg: 1.6500000000000001



The textbook weight in kg: 1.9000000000000001"



But the output i need is:



1.9 only,without the previous lines.



As I'm still a beginner, I know very little about the correct code. So any help will be appreciated










share|improve this question
























  • Can you elaborate on what exactly is the issue? Or the expected output?
    – dmitriys
    Nov 13 at 0:21










  • The expected output is 1.9kg
    – topu
    Nov 13 at 0:22










  • This is just floating point inaccuracy, you just need to round the numbers to something more appropriate (such as to two decimal places). You can do this by round(output, 2)
    – Polymer
    Nov 13 at 0:25










  • For clarification, floating point inaccuracy is explained here effbot.org/pyfaq/…
    – Polymer
    Nov 13 at 0:26










  • There is this very non beginner friendly answer stackoverflow.com/questions/588004/…
    – Paul Rooney
    Nov 13 at 0:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm practicing previous python exam question for an upcoming exam but my code is screwy.




  1. The task is to make a text file with a list of weights in grams (done).


  2. Prompts user for file name, reads the weights, adds them in a list and calculates the total weight.



Herein lies the problem:



try:

file = input('Enter file name:')

f = open('weights.txt', 'r')

sum=0

for line in f:

sum = sum+(int(line.strip()))/1000

print('The textbook weight in kg:', sum)

except:

print('File cannot be opened')


The output the programme shows is:



"The textbook weight in kg: 0.5



The textbook weight in kg: 0.65



The textbook weight in kg: 1.35



The textbook weight in kg: 1.6500000000000001



The textbook weight in kg: 1.9000000000000001"



But the output i need is:



1.9 only,without the previous lines.



As I'm still a beginner, I know very little about the correct code. So any help will be appreciated










share|improve this question















I'm practicing previous python exam question for an upcoming exam but my code is screwy.




  1. The task is to make a text file with a list of weights in grams (done).


  2. Prompts user for file name, reads the weights, adds them in a list and calculates the total weight.



Herein lies the problem:



try:

file = input('Enter file name:')

f = open('weights.txt', 'r')

sum=0

for line in f:

sum = sum+(int(line.strip()))/1000

print('The textbook weight in kg:', sum)

except:

print('File cannot be opened')


The output the programme shows is:



"The textbook weight in kg: 0.5



The textbook weight in kg: 0.65



The textbook weight in kg: 1.35



The textbook weight in kg: 1.6500000000000001



The textbook weight in kg: 1.9000000000000001"



But the output i need is:



1.9 only,without the previous lines.



As I'm still a beginner, I know very little about the correct code. So any help will be appreciated







python python-2.7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 0:25

























asked Nov 13 at 0:17









topu

12




12












  • Can you elaborate on what exactly is the issue? Or the expected output?
    – dmitriys
    Nov 13 at 0:21










  • The expected output is 1.9kg
    – topu
    Nov 13 at 0:22










  • This is just floating point inaccuracy, you just need to round the numbers to something more appropriate (such as to two decimal places). You can do this by round(output, 2)
    – Polymer
    Nov 13 at 0:25










  • For clarification, floating point inaccuracy is explained here effbot.org/pyfaq/…
    – Polymer
    Nov 13 at 0:26










  • There is this very non beginner friendly answer stackoverflow.com/questions/588004/…
    – Paul Rooney
    Nov 13 at 0:27


















  • Can you elaborate on what exactly is the issue? Or the expected output?
    – dmitriys
    Nov 13 at 0:21










  • The expected output is 1.9kg
    – topu
    Nov 13 at 0:22










  • This is just floating point inaccuracy, you just need to round the numbers to something more appropriate (such as to two decimal places). You can do this by round(output, 2)
    – Polymer
    Nov 13 at 0:25










  • For clarification, floating point inaccuracy is explained here effbot.org/pyfaq/…
    – Polymer
    Nov 13 at 0:26










  • There is this very non beginner friendly answer stackoverflow.com/questions/588004/…
    – Paul Rooney
    Nov 13 at 0:27
















Can you elaborate on what exactly is the issue? Or the expected output?
– dmitriys
Nov 13 at 0:21




Can you elaborate on what exactly is the issue? Or the expected output?
– dmitriys
Nov 13 at 0:21












The expected output is 1.9kg
– topu
Nov 13 at 0:22




The expected output is 1.9kg
– topu
Nov 13 at 0:22












This is just floating point inaccuracy, you just need to round the numbers to something more appropriate (such as to two decimal places). You can do this by round(output, 2)
– Polymer
Nov 13 at 0:25




This is just floating point inaccuracy, you just need to round the numbers to something more appropriate (such as to two decimal places). You can do this by round(output, 2)
– Polymer
Nov 13 at 0:25












For clarification, floating point inaccuracy is explained here effbot.org/pyfaq/…
– Polymer
Nov 13 at 0:26




For clarification, floating point inaccuracy is explained here effbot.org/pyfaq/…
– Polymer
Nov 13 at 0:26












There is this very non beginner friendly answer stackoverflow.com/questions/588004/…
– Paul Rooney
Nov 13 at 0:27




There is this very non beginner friendly answer stackoverflow.com/questions/588004/…
– Paul Rooney
Nov 13 at 0:27












1 Answer
1






active

oldest

votes

















up vote
4
down vote













Your problem is with your indentation. You should print your result after the whole for loop is executed, i.e.



try:
file = input('Enter file name:')
f = open('weights.txt', 'r')
sum=0
for line in f:
sum = sum+(int(line.strip()))/1000
print('The textbook weight in kg:', sum)
except:
print('File cannot be opened')


Also for the floating point inaccuracy thing, you could format your print like this:



print('The textbook weight in kg:{:.2f}'.format(sum))
# The textbook weight in kg:1.90





share|improve this answer





















  • Can you explain the format so that I can remember and understand when to apply?
    – topu
    Nov 13 at 0:46










  • @topu {} marks the position you want to replace with the argument of format, : means the argument is to be formatted, .2f means floating point with 2 digits precision. For mor usage please refer to some tutorial website such as pyformat.info
    – Kevin Fang
    Nov 13 at 1:21










  • P.S. next time don't use question title like this, see stackoverflow.com/help/how-to-ask
    – Kevin Fang
    Nov 13 at 1:23











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271992%2fpractice-python-task-for-exam%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote













Your problem is with your indentation. You should print your result after the whole for loop is executed, i.e.



try:
file = input('Enter file name:')
f = open('weights.txt', 'r')
sum=0
for line in f:
sum = sum+(int(line.strip()))/1000
print('The textbook weight in kg:', sum)
except:
print('File cannot be opened')


Also for the floating point inaccuracy thing, you could format your print like this:



print('The textbook weight in kg:{:.2f}'.format(sum))
# The textbook weight in kg:1.90





share|improve this answer





















  • Can you explain the format so that I can remember and understand when to apply?
    – topu
    Nov 13 at 0:46










  • @topu {} marks the position you want to replace with the argument of format, : means the argument is to be formatted, .2f means floating point with 2 digits precision. For mor usage please refer to some tutorial website such as pyformat.info
    – Kevin Fang
    Nov 13 at 1:21










  • P.S. next time don't use question title like this, see stackoverflow.com/help/how-to-ask
    – Kevin Fang
    Nov 13 at 1:23















up vote
4
down vote













Your problem is with your indentation. You should print your result after the whole for loop is executed, i.e.



try:
file = input('Enter file name:')
f = open('weights.txt', 'r')
sum=0
for line in f:
sum = sum+(int(line.strip()))/1000
print('The textbook weight in kg:', sum)
except:
print('File cannot be opened')


Also for the floating point inaccuracy thing, you could format your print like this:



print('The textbook weight in kg:{:.2f}'.format(sum))
# The textbook weight in kg:1.90





share|improve this answer





















  • Can you explain the format so that I can remember and understand when to apply?
    – topu
    Nov 13 at 0:46










  • @topu {} marks the position you want to replace with the argument of format, : means the argument is to be formatted, .2f means floating point with 2 digits precision. For mor usage please refer to some tutorial website such as pyformat.info
    – Kevin Fang
    Nov 13 at 1:21










  • P.S. next time don't use question title like this, see stackoverflow.com/help/how-to-ask
    – Kevin Fang
    Nov 13 at 1:23













up vote
4
down vote










up vote
4
down vote









Your problem is with your indentation. You should print your result after the whole for loop is executed, i.e.



try:
file = input('Enter file name:')
f = open('weights.txt', 'r')
sum=0
for line in f:
sum = sum+(int(line.strip()))/1000
print('The textbook weight in kg:', sum)
except:
print('File cannot be opened')


Also for the floating point inaccuracy thing, you could format your print like this:



print('The textbook weight in kg:{:.2f}'.format(sum))
# The textbook weight in kg:1.90





share|improve this answer












Your problem is with your indentation. You should print your result after the whole for loop is executed, i.e.



try:
file = input('Enter file name:')
f = open('weights.txt', 'r')
sum=0
for line in f:
sum = sum+(int(line.strip()))/1000
print('The textbook weight in kg:', sum)
except:
print('File cannot be opened')


Also for the floating point inaccuracy thing, you could format your print like this:



print('The textbook weight in kg:{:.2f}'.format(sum))
# The textbook weight in kg:1.90






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 0:32









Kevin Fang

1,083315




1,083315












  • Can you explain the format so that I can remember and understand when to apply?
    – topu
    Nov 13 at 0:46










  • @topu {} marks the position you want to replace with the argument of format, : means the argument is to be formatted, .2f means floating point with 2 digits precision. For mor usage please refer to some tutorial website such as pyformat.info
    – Kevin Fang
    Nov 13 at 1:21










  • P.S. next time don't use question title like this, see stackoverflow.com/help/how-to-ask
    – Kevin Fang
    Nov 13 at 1:23


















  • Can you explain the format so that I can remember and understand when to apply?
    – topu
    Nov 13 at 0:46










  • @topu {} marks the position you want to replace with the argument of format, : means the argument is to be formatted, .2f means floating point with 2 digits precision. For mor usage please refer to some tutorial website such as pyformat.info
    – Kevin Fang
    Nov 13 at 1:21










  • P.S. next time don't use question title like this, see stackoverflow.com/help/how-to-ask
    – Kevin Fang
    Nov 13 at 1:23
















Can you explain the format so that I can remember and understand when to apply?
– topu
Nov 13 at 0:46




Can you explain the format so that I can remember and understand when to apply?
– topu
Nov 13 at 0:46












@topu {} marks the position you want to replace with the argument of format, : means the argument is to be formatted, .2f means floating point with 2 digits precision. For mor usage please refer to some tutorial website such as pyformat.info
– Kevin Fang
Nov 13 at 1:21




@topu {} marks the position you want to replace with the argument of format, : means the argument is to be formatted, .2f means floating point with 2 digits precision. For mor usage please refer to some tutorial website such as pyformat.info
– Kevin Fang
Nov 13 at 1:21












P.S. next time don't use question title like this, see stackoverflow.com/help/how-to-ask
– Kevin Fang
Nov 13 at 1:23




P.S. next time don't use question title like this, see stackoverflow.com/help/how-to-ask
– Kevin Fang
Nov 13 at 1:23


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271992%2fpractice-python-task-for-exam%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?