beginner question about a code for converting inch(es) to cm
Hi, I have just started learning Python and am a total newbie. Besides SoloLearn I am using a book designed for beginners which came with a CD with an IDLE on it. I am already having problems with the 3rd exercise, which is to write a code for converting inch(es) to cm. inch=2.54 print("Please enter a value for inch.") xi=float(input()) xcm=xi*inch print(xi, "inch(es) equal", xcm, "cm") playground: https://code.sololearn.com/c7D39UG6OhIF The code seems to work in playground. It is also the sample solution given in the book (keep in mind, this is the very start of the book...). I had to look up the sample solution because I kept getting errors when creating own codes in the IDLE. Unfortunately, even the sample solution does not work in the IDLE: Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> xi=float(input()) ValueError: could not convert string to float: As the code works with playground, I am thinking that something is wrong with the IDLE. I hope you guys can help me.
5/11/2019 10:44:31 AM
Julian
9 Answers
New AnswerJulian The program you have written at codeplayground is correct. It's just that sololearn's input mechanism is different. IDLE is a shell so it will evaluate line by line. And you can enter any number either in IDLE or sololearn. To fully run the code offline you need to create a source file like you did it here. Then compile and run it from command line/terminal like > python inch_to_cms.py or from with in the IDE (editor+compiler) I think python installer comes with simple IDE from within which you can run your program
Julian Check the input again (probably you entered comma instead of dot), it's working fine in IDLE
Julian output from IDLE (from pydroid android app) >>> inch=2.54 >>> print("Please enter a value for inch.")Please enter a value for inch. >>> xi=float(input()) 2 >>> xcm=xi*inch >>> print(xi, "inch(es) equal", xcm, "cm") 2.0 inch(es) equal 5.08 cm >>>
Julian after >>> xi = float(input()) when you press enter you need to enter a value like 2. If you press enter it will be taken as empty string and you will get conversion error
Thank you so much ~ swim ~ and Игорь Яковенко for the fast answers :) Now it all makes sense and I can continue :)
>>> inch=2.54 >>> print("Please enter a value for inch.") Please enter a value for inch. >>> xi=float(input()) Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> xi=float(input()) ValueError: could not convert string to float: >>> This is the complete code until receiving an error message in the IDLE. To me it seems to be the exact same I entered in playground... Although playground does not have (or display) ">>>". In playground, I only had to press enter key once, in the IDLE i had to use it twice to get the next ">>>". P.S.: I do not know the different terms yet, sry.
Oh, I see, this works. But how can I write the full code (https://code.sololearn.com/c7D39UG6OhIF) without having to enter a number? I want to be able to enter any number after it is finished. By entering a number while writing the code, it is fix, isn´t it?