-2

Hello wonderful people of stackoverflow. I am new to Python and just wrote my second program. It works but I want it to be better. Basically, I made a program that converts distance measurements. What I would like to do is only take inputs that are in my list. Then only take digits. Then only take inputs from my list again. I cannot find any help and would really appreciate it. Here are the initial lines of the code. I will not include the rest as it is an additional few dozen lines doing all the math. Thank you.

print("Meters | Feet | Yards | Miles | Kilometers | Inches | Centimeters")
measure1 = input("What would you like to measure? ")
measure1insensitive = (measure1.casefold())
num1 = input("How much? Enter digit: ")
num1_int = int(num1)
print("Meters | Feet | Yards | Miles | Kilometers | Inches | Centimeters")
measure2 = input("What do you want to convert this to? ")
measure2insensitive = (measure2.casefold())
  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Pranav Hosangadi Sep 13 '20 at 05:19

2 Answers2

0

In Python you can use the handy function in.

If you have a list, saying "my string" in ["list",'with','strings'] will return False because "my string" does not match any of the list items.

Similarly in strings, "my" in "my string" returns True because it is a substring.

If you want to be lazy and convert your current string with all options to a list (which would prevent someone entering "In" and "In" in "Inches" is True which is not what you'd want) the you can take the output of "My | string | with | options".split('|').strip() to break on the '|' characters and strip whitespace from either ends of the string.

delta95
  • 16
  • 4
0

We can create a list of all the accepted measurement categories. Then we can put all prompts to the input measurement category in an infinite loop that breaks when the user enters an acceptable category. We check this by using the "in" operator. Then we can use .idex() function of the List to identify the index of the measurement category and you can do your logic using the indices.

   measurments = ["meters","feet", "yards", "miles","kilometers","inches","centimeters"]
print("Meters | Feet | Yards | Miles | Kilometers | Inches | Centimeters")
while True:
    measure1 = input("What would you like to measure? ")
    measure1insensitive = (measure1.casefold())
    if measure1insensitive in measurments:
        break

m1 = measurments.index(measure1insensitive)
while True:
    num1 = input("How much? Enter digit: ")
    num1_f = float(num1)
    if (num1_f.is_integer()):
        break
num1_int = int(num1_f)
print("Meters | Feet | Yards | Miles | Kilometers | Inches | Centimeters")
while True:
    measure2 = input("What do you want to convert this to? ")
    measure2insensitive = (measure2.casefold())
    if measure2insensitive in measurments:
        break
m2 = measurments.index(measure2insensitive)
# Your logic
if m1==0: # From meters
   if m2==1: # To feets
        print("do the logic")
gihan
  • 141
  • 5
  • I modified the answer, in the case user enters a wrong text, it will display the prompt again. – gihan Sep 13 '20 at 05:30
  • Thank you. This is great. One more question, how do I make the num1 input forced to only be an integer and have the same loop if the user does not input an integer? Thank you again! – user14267912 Sep 13 '20 at 16:58
  • You can follow the same while loop pattern to prompt the user to enter integers. – gihan Sep 13 '20 at 17:06
  • Please accept the answer if it works for you :) so it helps to build my reputation. – gihan Sep 13 '20 at 17:08
  • I modify the answer to include a loop for integer input checks. I modify it to initially accept float datatype then use the is_integer() method to check the input is actually an integer value even though the data type is float. – gihan Sep 13 '20 at 17:21
  • How would I loop back to ask the user to put in an integer if they do not put one in? – user14267912 Sep 13 '20 at 17:51
  • In this code, it will keep prompting the user to enter digits if they do not enter an integer – gihan Sep 13 '20 at 17:53
  • I tried running it, but there is an error message: What would you like to measure? miles How much? Enter digit: eagles Traceback (most recent call last): File "/Users/mymac/PycharmProjects/Converter/main.py", line 12, in num1_f = float(num1) ValueError: could not convert string to float: 'eagles' – user14267912 Sep 13 '20 at 17:55
  • In that case, you will have to add exception handling. https://stackoverflow.com/questions/8420143/valueerror-could-not-convert-string-to-float-id – gihan Sep 13 '20 at 18:02
  • Figured it out for those that care. Your while True formula helped gihan. while True: num1 = input("How much? Enter digit: ") if num1.isdecimal(): break print("Please enter a digit."). It was the .isdecimal(): that did it. – user14267912 Sep 27 '20 at 20:39