-3

I'm new to functions and arguments (actually new to python in general) and I'm having a bit of a problem making this program multiply the unit given by the user and output it. It just keeps coming up as whatever the variable is up top, and if I don't include that it says it isn't defined. May someone help, please?

# Variables

    inches = 0
    item_length = 0
    unit_value = 0

# Your unit (smooots for general purposes) function
    def inches_to_smoots(inches):

    
##      inches = item x unit value
        inches = item_length * unit_value
        
##      return the number of inches
        return inches

## main function
def main():
    
        unit = input("What is the name of your unit? ")
        
        unit_value = input (str("What is the length of your unit in inches? "))
        
        item = input("What is the name of the object you'd like to convert to your unit? ")

        item_length = input ("What is the length of your item in inches? ") # Is there a way to print a variable inside an input statement?
                             
        answer = inches_to_smoots(item_length)
                             
##      print the answer
        print(item_length,'inches is', inches, unit, 's!')
                            
## call main
main()

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 2
    Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) Also: note that you don't need the `str()` in `unit_value = ...` because anything inside quotes is _already_ a string. – Pranav Hosangadi Dec 01 '21 at 16:14

2 Answers2

1

Instead of declaring the variables up at the top, your function should take the necessary inputs as arguments. Since you're doing unit conversion, it's very helpful to give each variable a name that indicates what units it's in! For example, your inches_to_smoots function is presumably (according to its name) supposed to take an inch measurement and return a smoot measurement, but you're returning a value called inches. It would make more sense for one of its arguments to be inches and for it to return something that's in smoots. When your code itself makes sense, you'll find that it's not necessary to comment each individual line to explain it.

Make sure to keep the Python types straight as well -- input() gives you a str, but for any kind of mathematical operation you want values to be int or float.

def inches_to_smoots(inches: float, smoots_per_inch: float) -> float:
    smoots = inches * smoots_per_inch
    return smoots


def main():
    smoot = input("What is the name of your unit? ")
    inches_per_smoot = float(input(
        f"What is the length of one {smoot} in inches? "
    ))

    item_name = input(
        f"What is the name of the object you'd like to convert to {smoot}s? "
    )

    item_inches = float(input(f"What is the length of your {item_name} in inches? "))
    item_smoots = inches_to_smoots(item_inches, 1 / inches_per_smoot)

    print(f"{item_inches} inches is {item_smoots} {smoot}s!")


if __name__ == '__main__':
    main()

Note that your function inches_to_smoots expects to be told how many smoots are in an inch, but you asked the user for the number of inches in a smoot -- hence you want to take the reciprocal! smoots_per_inch == 1 / inches_per_smoot.

Example output:

What is the name of your unit? finger
What is the length of one finger in inches? 4.5
What is the name of the object you'd like to convert to fingers? hand
What is the length of your hand in inches? 7
7.0 inches is 1.5555555555555554 fingers!
Samwise
  • 68,105
  • 3
  • 30
  • 44
  • Thanks so much for this help! I love how helpful your answer is, and the tips you gave to assist me in the future! Thank you very much for your time! – Basedgod Dec 01 '21 at 16:51
0

To make math with elements inputed by the user, you need to convert it to int or float using the int() or float() methods.

        unit = input("What is the name of your unit? ")
        
        unit_value = int(input (str("What is the length of your unit in inches? ")))
        
        item = int(input("What is the name of the object you'd like to convert to your unit? "))

        item_length = int(input ("What is the length of your item in inches? ") # Is there a way to print a variable inside an input statement?)

That is because it is seeing inputs as strings and python does not know how to properly multiply those together.