Error converting input string. What is wrong here? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

Error converting input string. What is wrong here?

ŠŸŠ¾Š»ŃŒŠ·Š¾Š²Š°Ń‚ŠµŠ»ŃŒ Š²Š²Š¾Š“Šøт чŠøсŠ»Š¾, Š½ŠµŠ¾Š±Ń…Š¾Š“ŠøŠ¼Š¾ Š²Ń‹ŃŃ‡ŠøтŠ°Ń‚ŃŒ ŠŗŠ¾Ń€ŠµŠ½ŃŒ этŠ¾Š³Š¾ чŠøсŠ»Š°. ŠšŠ°Šŗ ŠøсŠæрŠ°Š²Šøть ŠŗŠ¾Š“? The user enters a number, need to convert number to sqrt. How to fix the code? num = float(input()) import math num2 = math.sqrt(num) Š’Ń‹Š“Š°ŠµŃ‚, чтŠ¾ Š½ŠµŃƒŠ“Š°Š»Š¾ŃŃŒ ŠæрŠµŠ¾Š±Ń€Š°Š·Š¾Š²Š°Ń‚ŃŒ стрŠ¾Šŗу Š² чŠøсŠ»Š¾ с ŠæŠ»Š°Š²Š°ŃŽŃ‰ŠµŠ¹ тŠ¾Ń‡ŠŗŠ¾Š¹ (Š’ ŠæŠµŃŠ¾Ń‡Š½ŠøцŠµ Š²Ń€Š¾Š“Šµ Š±Ń‹ рŠ°Š±Š¾Ń‚Š°ŠµŃ‚, Š° Š½Š° ŠŗŠ¾Š¼ŠæьютŠµŃ€Šµ срŠ°Š·Ńƒ Š²Ń‹Š“Š°ŠµŃ‚ эту Š¾ŃˆŠøŠ±Šŗу) ValueError: could not convert string to float: 'num2 = math.sqrt(num)'

18th Oct 2019, 2:42 PM
Apel'sinka
9 Respostas
+ 5
hi, i took the code as it is from you, and it could run. Not running properly, but without Error message. the cod as povided by you is running in an infinite loop, as num2 is a float after calculating math.sqrt(num). so the result of modulo division if num2 % 10 == 0 will never evaluate True. and so the program can not terminate. but anyway, there must be a difference between the code that causes an error for you, and the code that is presented here. i have put it now with my suggestions together in a file, so that you can run it in playground. i have inserted an additional print statement,just to visualize what happens. https://code.sololearn.com/cCd2MMp9Sxyv/?ref=app
20th Oct 2019, 5:43 PM
Lothar
Lothar - avatar
+ 5
hi, i think that the problem is, that this line of code : num2 = math.sqrt(num) (in else clause) always is producing a float, that never will match the if clause for break. so changing code to this, will solve the problem: num2 = int(math.sqrt(num)).
18th Oct 2019, 4:45 PM
Lothar
Lothar - avatar
+ 2
There's no problem with thw code. Probably user haven't entered a valid number
18th Oct 2019, 2:53 PM
Ī›M!N
Ī›M!N - avatar
+ 2
Are you sure? The code works for me properly.
18th Oct 2019, 3:01 PM
Ī›M!N
Ī›M!N - avatar
+ 1
No, the number's normal. Python displays an error immediately when the code runs.
18th Oct 2019, 3:00 PM
Apel'sinka
+ 1
The next time the program was checked, an error occurred (the code did not change): (1 line) SyntaxError: myltiple statments found while compiling a single statments This's obtained on the computer. In the "ŠæŠµŃŠ¾Ń‡Š½ŠøцŠ°" (sandbox), it doesn't start at all The code: https://code.sololearn.com/c7L1i8EbiFv2/?ref=app
18th Oct 2019, 3:18 PM
Apel'sinka
+ 1
This has not changed anything. The error still appears: (1 line) SyntaxError: multiple statements found while compiling a single statements
20th Oct 2019, 4:42 PM
Apel'sinka
+ 1
Thank you. The problem was with the python compiler on my computer
20th Oct 2019, 5:54 PM
Apel'sinka
+ 1
During the written code, I accidentally wrote 10 instead of 1, so endless code came out
20th Oct 2019, 5:58 PM
Apel'sinka