что-то не так с моим циклом while внутри моей функции eval_strfrac (s, base = 2). для 3.14 базы 10 это очень близко, для 100.101 базы 2 это далеко. Спасибо!
#TEST to validate
def is_valid_strfrac(s, base=2):
return all([is_valid_strdigit(c, base) for c in s if c != '.']) \
and (len([c for c in s if c == '.']) <= 1)
def eval_strfrac(s, base=2):
assert is_valid_strfrac(s, base), "'{}' contains invalid digits for a base-{} number.".format(s, base)
#
predot,postdot = s.split('.')
whole = eval_strint(predot,bse)
whole = int(predot,base)
postlist = [int(p) for p in postdot]
print(postlist)
i = 0
while i <= len(postlist):
yo = (postlist[i])*((float(base))**-(float(i + 1)))
yo += yo
i +=1
return float(whole) + float(yo)
#### Test 0: `eval_strfrac_test0`#####
def check_eval_strfrac(s, v_true, base=2, tol=1e-7):
v_you = eval_strfrac(s, base)
assert type(v_you) is float, "Your function did not return a `float` as instructed."
delta_v = v_you - v_true
msg = "[{}]_{{{}}} ~= {}: You computed {}, which differs by {}.".format(s, base, v_true,
v_you, delta_v)
print(msg)
assert abs(delta_v) <= tol, "Difference exceeds expected tolerance."
# Test cases from the video
check_eval_strfrac('3.14', 3.14, base=10)
check_eval_strfrac('100.101', 4.625, base=2)
check_eval_strfrac('11.0010001111', 3.1396484375, base=2)
# A hex test case
check_eval_strfrac('f.a', 15.625, base=16)
print("\n(Passed!)")
[3.14] _ {10} ~ = 3.14: Вы вычислили 3.2, что отличается на 0.06000000000000005.
[100.101] _ {2} ~ = 4.625: Вы вычислили 5.0, что отличается на 0.375.