Python Forum
Division calcuation with answers to 1decimal place.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Division calcuation with answers to 1decimal place.
#1
I'm trying to write a program that takes two random integers from 0-10 and divides them by each other and have the user enter the answer to one decimal place.

I keep on getting incorrect even though I think I'm trying in the correct answer.

The code is below. Please help.

import random

first = random.randint(0,10)
second = random.randint(0,10)

symbol = " / "
print(first, symbol, second, "=")

answer = first / second
answer = ("{:.1f}".format(answer)) # division answer to 1 decimal place

print(answer) # The print is for testing purposes.

user_answer = float(input("enter you answer to one decimal place"))
  # mark answer
if user_answer == answer:
    # correct
    print("Correct!")
else:
    # incorrect
    print("Incorrect!")
Reply
#2
You are comparing a str (answer) and a float (user_answer). They will never be equal. Leave the user input as a str and compare the strings.
sik likes this post
Reply
#3
(Jul-14-2021, 03:57 PM)deanhystad Wrote: You are comparing a str (answer) and a float (user_answer). They will never be equal. Leave the user input as a str and compare the strings.

Thank you so much!
Reply
#4
some_value = 1.337
guessed_value = 1.3

# round(value, decimal_places)

if round(some_value, 1) == round(guessed_value, 1):
    print("Rounded values are equal")
If you made a calculation and want to compare if the two values are equal, you should use math.isclose.

import math


value_a = 1337 * 1.3 / 42
value_b = 1337 / 42 * 1.3

print("Value_a:", value_a)
print("Value_b:", value_b)

if value_a != value_b:
    print("The values are not equal")
    
if math.isclose(value_a, value_b):
    print("But the values are close enough")
Quote:Value_a: 41.38333333333334
Value_b: 41.38333333333333
The values are not equal
But the values are close enough

Just changing the order of calculation, will result in a different value.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  'answers 2' is not defined on line 27 0814uu 4 748 Sep-02-2023, 11:02 PM
Last Post: 0814uu
  place 2 windows exactly above each other janeik 3 934 Jul-23-2023, 03:12 AM
Last Post: deanhystad
  Division questions Dionysis 5 1,058 Feb-14-2023, 02:02 PM
Last Post: Dionysis
  Division by zero and value of argument Lawu 5 3,151 Jul-01-2022, 02:28 PM
Last Post: Lawu
  Floor division problem with plotting x-axis tick labels Mark17 5 2,118 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Cannot 'break' from a "for" loop in a right place tester_V 9 4,001 Feb-17-2021, 01:03 AM
Last Post: tester_V
  Cannot Assign right Answers To Shuffled Questions Boblows 6 2,776 Jan-22-2021, 09:41 AM
Last Post: buran
  Floor division return value Chirumer 8 3,797 Nov-26-2020, 02:34 PM
Last Post: DeaD_EyE
  Integer division plozaq 2 1,997 Sep-28-2020, 05:49 PM
Last Post: plozaq
  Overcoming ZeroDivisionError: division by zero Error dgrunwal 8 5,048 Jun-12-2020, 01:52 PM
Last Post: dgrunwal

Forum Jump:

User Panel Messages

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