Division problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Division problem (/thread-22523.html) |
Division problem - Eric7Giants - Nov-16-2019 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 When I run this block of code, though, it only prints 0. What am I missing here? RE: Division problem - ndc85430 - Nov-16-2019 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).
|