Python Forum
How to except a repeating number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to except a repeating number
#1
Ok..I am making a math tutor program...student chooses a process (add, sub, div, mul), then enters first number, second number, and then answer.

This works for division...except when the answer is a number such as 3.3333333 or .333333. If I input 3.33 or .33 the program says the answer is incorrect and then shows the correct answer at 3.33 or .33. The program is supposed to show round to two decimal points.

My question, how to I make it so the program excepts the rounded number? (note... all the elements that you see are needed...enter first number, enter second number, enter answer, correct or incorrect response with the correct answer) I finally figured out how to make it loop back to the question if a zero is entered)

#division calculation
        elif choice == divF:
            while (num1 := int(input('Enter first number: '))) == 0:
                print ('Number must not be a zero.')
                    
            while (num2 := int(input("Enter second number: "))) == 0:
                print ('Number must not be a zero.')

            answer = float(input("Enter answer (to 2 decimal points): "))

            c_answer = float(num1 / num2)
#output
            if c_answer == answer:
                print("Correct, great job!")
            else:
                print("Incorrect, the answer is ", f'{c_answer:.2f}',".")
Reply
#2
You seemingly want imprecise numbers comparison, right?

It's mainly up to you to define how much imprecision you allow. General approach is that you take two values to be compared, find their
difference and check this difference (or rather its absolute value) is below certain fraction (say, 0.01) of the target value.
Reply
#3
Not sure what your problem is exactly. Are you guys all in the same course? There was such a division question just recently.

You should talk to each other!

This seems to work:

import re
# find a float
g = re.compile(r'\A(?=\d+(.\d+)?\Z).*')
# use h to look for at least 1 number between 1 and 9 in the divisor
# if not h, there are just zeroes and division will be difficult!
h = re.compile(r'[1-9]+')
def do_division():
    while True:
        print('Attention attention!')
        print('Only integers or floats allowed: number or number.number, no other characters.')
        nums = input('Enter your numbers, separated by a space ... ').split()        
        if not h.search(nums[1]):
            print('And the second number must contain at least 1 number > 0 ... ')
            continue        
        # check for any non-number characters
        elif g.match(nums[0]) and g.match(nums[1]):
            result = float(nums[0]) / float(nums[1])
            print(f'{nums[0]} / {nums[1]} = {result}')
            break
        else:
            print('The numbers you entered do not have the correct format, try again ... ') 
    return (round(result, 2), nums[0], nums[1])
Give this output:

Output:
res = do_division() Attention attention! Only integers or floats allowed: number or number.number, no other characters. Enter your numbers, separated by a space ... 6.666666 2 6.666666 / 2 = 3.333333 res (3.33, '6.666666', '2')
Then check the guess:

guess = input(f'How much is {res[1]} divided by {res[2]}, rounded to 2 decimal places? ')
if float(guess) == res[0]:
    print(f'Clever clogs!')
else:
    print('Arithmetic not your strong point I see ... ')
Reply
#4
I actually figured this out like 30mins after posting this.

What I did...

line 11

c_answer = float(num1 / num2)

Changed it to read....

c_answer = round (float(num1 / num2),2)

That was all it took to make it work. I started working on this thing two weeks ago, had it all figured out except for this and the not excepting a zero.

I am happy to say, I had everything I was expected to add to it..now just waiting on a grade.
Thank you for the help :)
Pedroski55 likes this post
Reply
#5
Hope your grade is good!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  repeating pastakipp 1 1,645 Oct-24-2020, 03:28 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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