간단한 제곱근 계산기를 만들고 싶었습니다.파이썬 스퀘어 루트 계산기 오류
num = input('Enter a number and hit enter: ')
if len(num) > 0 and num.isdigit():
new = (num**0.5)
print(new)
else:
print('You did not enter a valid number.')
내가 잘못 아무 짓도 한 것처럼은 보이지 않는다
그러나, 나는 프로그램을 실행하려고 내가 숫자 입력을 한 후, 나는 다음과 같은 오류 메시지가 직면하고 때
Traceback (most recent call last):
File "/Users/username/Documents/Coding/squareroot.py", line 4, in <module>
new = (num**0.5)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float'
Process finished with exit code 1
당신은 입력 * * 숫자로 변환 할 수 있다면 신중하게 다음, 확인 * 실제로 그렇게 귀찮게하지 않습니다 * : 은 참조! – jonrsharpe
또한 '1.5'또는 '1E10'과 같은 숫자는 유효하지 않습니다 - 이유는 무엇입니까? 파이썬적인 방법은 입력을'float'로 변환하려고 시도하고 예외가 발생할 때만 오류 메시지를 출력하는 것입니다. –
가능한 [TypeError : - : 'str'및 'int'에 대해 지원되지 않는 피연산자 유형] (http://stackoverflow.com/questions/2376464/typeerror-unsupported-operand-types-for-str-and) -int) –