Welcome to the community :). A few pointers:
- The code does not follow PEP-8 style guide. You should follow the
snake_case
naming convention for variables.
- Split code into separate functions.
- Instead of the code running as is, the execution condition should be placed inside an
if __name__
block.
- If you're on python 3.6+, make use of f-strings over concatenating separate variables and casting them to strings over and over.
- Cast the input from user as float as soon as it is read.
- I don't think you need to round the random variable calculated to 4 digits. For print purpose, you can use the precision specifier.
- Again, if using python 3.6+, you can also provide type hinting.
The above mentioned points, and a few more changes, your code would be:
from math import isclose
from random import randint
def calculate_sq_root(num: float) -> float:
"generate a random number and manipulate it until we calculate square root of input number"
random_num = randint(1, 9)
print(f"Random number is {random_num}.")
while not isclose(num, random_num * random_num):
random_num = (random_num ** 2 + num) / (random_num * 2)
print(f"Trying number {random_num:.4f}")
return random_num
def get_input() -> float:
"ask user for a number to find sq root for"
return float(input("Please enter number for which we'll find sq root:\n"))
def main():
user_num = get_input()
sq_root = calculate_sq_root(user_num)
print(f"Square root of {user_num} is {sq_root}.")
if __name__ == "__main__":
main()