+ 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