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.
The task is to make a text file with a list of weights in grams (done).
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
|
show 1 more comment
up vote
0
down vote
favorite
I'm practicing previous python exam question for an upcoming exam but my code is screwy.
The task is to make a text file with a list of weights in grams (done).
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
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 byround(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
|
show 1 more comment
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.
The task is to make a text file with a list of weights in grams (done).
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
I'm practicing previous python exam question for an upcoming exam but my code is screwy.
The task is to make a text file with a list of weights in grams (done).
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
python python-2.7
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 byround(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
|
show 1 more comment
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 byround(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
|
show 1 more comment
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
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 offormat
,:
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
add a comment |
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
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 offormat
,:
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
add a comment |
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
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 offormat
,:
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
add a comment |
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
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
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 offormat
,:
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
add a comment |
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 offormat
,:
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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