Python Forum

Full Version: PLEASE HELP! Basic Python Program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, Im looking for some help with my program, I have been set a task to make a basic Strain Calculator.

I need it to input two numbers, one being the 'change in Length' and the other being 'Original Length'.

Both the 'Change in Length' and 'Original Length' must have the choice of either Metres or Inches. Then the calculator must divide the 'Change in Length' by the Original Length' and the answer must have the option for the user to display the answer in Metres or Inches.

I am really struggling with this as completely new to Python.



this is what I have come up with so far... If someone could amend this and please make it work that would be amazing.
Thanks in advance.


txt = "Strain Calculator"
x = txt.title()
print(x)

# This function divides two numbers
def divide(x, y):
return x / y

print("Select operation.")
print("1.Strain")

while True:
# Take input from the user
choice = input("Enter choice(1): ")

if choice in ('1'):
num1 = float(input("Change in Length: "))
num2 = float(input("Original Length: "))

if choice == '1':
print(num1, "/", num2, "=", divide(num1, num2))
break
else:
print("Invalid Input")
Here is one way to go about it:

def get_numeric_input (prompt: str) -> float :
	while True :
		answer = input (prompt)
		try :
			return float (answer)
		except : pass

while True :
	print ("Strain Calculator")
	print ("Menu")
	print ("1) Strain")
	print ("2) Quit Now")
	answer = input ("Enter your choice : ")
	print ()
	if answer == "1" :
		change = get_numeric_input ("Enter change in length : ")
		original = get_numeric_input ("Enter original length : ")
		print (f"The answer is {change / original}\n")
	elif answer == "2" :
		exit ()
Thank you for your time, this would be perect if it had the choice of metres or inches and could calculate the answer to be in metres of inches, if you could implement this that would be amazing.

thank you




(Apr-10-2021, 01:11 PM)BashBedlam Wrote: [ -> ]Here is one way to go about it:

def get_numeric_input (prompt: str) -> float :
	while True :
		answer = input (prompt)
		try :
			return float (answer)
		except : pass

while True :
	print ("Strain Calculator")
	print ("Menu")
	print ("1) Strain")
	print ("2) Quit Now")
	answer = input ("Enter your choice : ")
	print ()
	if answer == "1" :
		change = get_numeric_input ("Enter change in length : ")
		original = get_numeric_input ("Enter original length : ")
		print (f"The answer is {change / original}\n")
	elif answer == "2" :
		exit ()
We aren't going to do the work for you. What have you tried and where are you stuck?
I can apricate that, this isn't homework or anything, I'm trying to learn phython, i have tried a few ways, where I am struggling is how to implement the Metres and Inches option for both 'change in length' and original length', i need an option for metres or inches for both of them and then the calculation to offer the answer in either metres or inches.

I know that 1 metre = 37.3701 inches and 1 inch = 0.0254 meter but just cant figure out how to implement that into the program.

thanks for your time.

(Apr-10-2021, 01:51 PM)ndc85430 Wrote: [ -> ]We aren't going to do the work for you. What have you tried and where are you stuck?
BTW - length divided by length gives you a ratio. There are no units. The meters or inches "cancel out". So, as long as the user is consistent, be it meters, inches, parsecs, or angstroms, the ratio is simply the one measurement divided by the other. No conversion necessary