Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (anonymous):

PYTHON HELP PLEASE I have to ask for someone to enter the number of inches of fabric they want to purchase, then i have to print it back to them saying you purchased "# of yards, # of feet, # of inches of material" it keeps telling me "Can't convert 'int' object to str implicitly" can anyone help me out with this?

OpenStudy (anonymous):

``` inches = eval(input("How many inches of material? ")) cost = eval(input("What is the cost (per yard)? ")) age = eval(input("What is your age? ")) #Start Math yards = inches // 36 #End Math print("You bought " + yards + "yards of material") ``` This is my code that ive got so far..

OpenStudy (anonymous):

@cwrw238 - any chance you would know some stuff about python?

OpenStudy (anonymous):

Any ideas?

OpenStudy (e.mccormick):

``` >>> x = 1 >>> print ("a" + x + "b") Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> print ("a" + x + "b") TypeError: cannot concatenate 'str' and 'int' objects >>> print ("a" + str(x) + "b") a1b >>> ```

OpenStudy (anonymous):

ooh alright let me try that

OpenStudy (anonymous):

thank you so much mccormick! it worked

OpenStudy (e.mccormick):

Yah, it is because of the type mismatch in the print. You were trying to do string manipulations on a number.

OpenStudy (anonymous):

ahh alright, so after doing yards, i have to make it take whats left of the inches and convert to feet.

OpenStudy (e.mccormick):

In all programming languages, type casting (changing from one type to another) is a real concern. Some play nice about it, some do not. And yah, you use the remainder to find what has been \(\textbf{not}\) divided off into yards and work with that.

OpenStudy (e.mccormick):

You using Python 3.x?

OpenStudy (e.mccormick):

The // makes me think you are using 3.x. Here is an example in Python 3.3.2. ``` >>> x=123 >>> y1=x/7 >>> y2=x//7 >>> y1 17.571428571428573 >>> y2 17 >>> z=x%7 >>> z 4 >>> 17*7+4 123 >>> ```

OpenStudy (anonymous):

@e.mccormick this is what i would think it woiuld be but it doesnt work. what would you suggest? to get the feet value ``` inches = eval(input("How many inches of material? ")) cost = eval(input("What is the cost (per yard)? ")) age = eval(input("What is your age? ")) #Start Math yards = inches // 36 feet = (inches % 36) // 12 #End Math print("You bought " + str(yards) + " yards, " + str(feet) + " feet of material") ```

OpenStudy (anonymous):

nvm it worked

OpenStudy (anonymous):

@e.mccormick i am however curious if this is the best way to do this?

OpenStudy (anonymous):

and yes its python 3.4.1

OpenStudy (e.mccormick):

It is not necessarily a bad way. It really depends on the approach you want to take. See, if keeping the input inches is not critical you could update it as you took out the yards, etc. Then the following formulas would be simpler. For three items, yd-ft-in, not a big deal. It is not like you are doing thousands of thousands of calculations where optimization is critical.

OpenStudy (anonymous):

hmm yeah i see what you mean, let me see if i can figure out the code to do that other way and get back to you

OpenStudy (anonymous):

cant get it to work, mind giving me a hint?

OpenStudy (e.mccormick):

With doing where you change the inches as you go you can do it one of two ways. Use remainder to update the inches or use yards * 36 as something you subtract from the inches to update it.

OpenStudy (anonymous):

alright let me give it a shot

OpenStudy (anonymous):

am i supposed to be doing this with an if statement?

OpenStudy (e.mccormick):

No need to. Not sure of you are getting what I meant. Want to see?

OpenStudy (anonymous):

yes i im kinda confused.

OpenStudy (e.mccormick):

``` >>> ## Just some examples >>> inches = 987 >>> yards = inches // 36 >>> inches = inches % 36 >>> feet = inches // 12 >>> inches = inches % 12 >>> yards 27 >>> feet 1 >>> inches 3 >>> 27 * 36 + 1 * 12 + 3 987 >>> ## OR: >>> inches = 987 >>> yards = inches // 36 >>> inches = inches - yards * 36 >>> feet = inches // 12 >>> inches = inches - feet * 12 >>> yards 27 >>> feet 1 >>> inches 3 >>> ## And for small values: >>> inches = 11 >>> yards = inches // 36 >>> yards 0 >>> ## it will still work ```

OpenStudy (anonymous):

oooh ok so i redo the value of inches after each one

OpenStudy (e.mccormick):

Yes. This only works if you don't need to save the original value. In this case, you are basically changing one number into 3 numbers that mean the same thing. Because the three numbers are more useful, the original number can safely be changed. But what if you needed to reuse inches later? Then you might input in_inches and then change that into yards, feet, inches. Then you would have 4 values. 3 for printout and the original for other purposes.

OpenStudy (anonymous):

AWESOME WOW I GOT IT :d

OpenStudy (anonymous):

ahh i see what you mean, that makes sense now

OpenStudy (e.mccormick):

And both ways work. What you did to get yards and feet is 100% valid. In programming there is almost always half a dozen simple ways to get something done. The question becomes if one is easier or one makes more sense to you. In cases where speed is important, the fastest way is best. In ways where RAM is at a premium, the fastest way may take too much ram and another method might be best. And for really simple things, like this example you are doing, the question is really what makes sense to you.

OpenStudy (anonymous):

yeah i see waht you mean, i just knew that wasnt how my teacher went over it, i was trying to figure out what she did. turns out what you told me was what she did so im glad you came help me :d

OpenStudy (e.mccormick):

Hehe. Well, now you have seen 3 ways. I did two that are very similar and you did another. All three got the same answer. So not like any are truly "wrong" unless the question says something specific. Like if the question says, "Use the modulo/remainder function to..." then you MUST you % because the question says so.

OpenStudy (anonymous):

yeah exactly. i think the way we did last is the easiest to read though, i like to have easy to read code if i can find a way of doing so

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!