Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Division problem
#1
Hello. I'm working on a problem where I ask a user for input on 3 test scores in the format "score/max" and then I am to find the average. I am running into trouble with the code though. I attempted to break the code down to find the error. Here is what I have so far:

*** Note this is python 2.7. I know it's obsolete but the assignment requires it to be done in 2.7***

#!/usr/bin/python
import math
import string
from string import split

first = raw_input("Please Enter your First exam score in the form score/max: ")
first_numerator = eval(first.split("/")[0])
first_denom = eval(first.split("/")[1])
first_score = ((first_denom / first_numerator))
print(first_score)
I checked the first_denom and first_numerator to make sure it is splitting up the scores correctly and they are.

Quote:Please Enter your First exam score in the form score/max: 96/100
96 #first_numerator
100 #first_denom

When I run this block of code, though, it only prints 0. What am I missing here?
Reply
#2
In Python 2, you get integer division by default (i.e. any fractional part is thrown away). If you want floating point division, you have to make one of the operands a float.

Also, on lines 7 and 8, there's no need to use eval - just use int (or float regarding the above).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Zero Division Error Leo 2 1,245 Mar-25-2022, 05:56 AM
Last Post: Leo
  Finding square roots using long division. jahuja73 10 5,420 Feb-24-2021, 01:25 PM
Last Post: jahuja73
  Count how many carpets you need to fill room floor without multiplication/division Ech0ke 1 2,315 Apr-20-2019, 07:50 PM
Last Post: ichabod801
  Zero Division Error moga2003 4 3,084 Mar-07-2019, 02:15 AM
Last Post: moga2003

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020