Python Forum
PLEASE HELP! Basic Python Program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PLEASE HELP! Basic Python Program
#1
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")
Yoriz write Apr-10-2021, 11:59 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I added code tags for you but the indentation of the code is missing
Reply
#2
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 ()
Reply
#3
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 ()
Reply
#4
We aren't going to do the work for you. What have you tried and where are you stuck?
joeroffey likes this post
Reply
#5
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?
Reply
#6
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
joeroffey likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 554 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  trying to understand a string literal in a basic server program CompleteNewb 4 2,105 Nov-14-2021, 05:33 AM
Last Post: deanhystad
  Basic python Natters10 3 3,081 Nov-29-2020, 07:04 AM
Last Post: Love2code
  Help with basic python AaronG123 4 2,251 Nov-14-2019, 02:57 PM
Last Post: AaronG123
  Python basic program webshakeup 4 2,769 Sep-19-2019, 07:14 AM
Last Post: Maheshsharma
  Heating program to translate from Basic to Python tommy2k19 12 5,955 May-21-2019, 08:16 AM
Last Post: tommy2k19
  Very basic programming help needed: pig latin program bstocks 2 7,989 Dec-02-2017, 08:12 AM
Last Post: RickyWilson

Forum Jump:

User Panel Messages

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