+ 1
Please when I input a number in this code it stil tells me "That is not a valid number". What could be wrong? I am a beginner.
https://code.sololearn.com/cxUJ6jenJPY1/?ref=app https://code.sololearn.com/cxUJ6jenJPY1/?ref=app
8 ответов
+ 2
Change
radius = input("Enter radius: ")
to
radius = int(input("Enter radius: "))
- When you use input(), what is stored in the variable is of 'str' datatype, not 'int'. Casting the variable to an int first will do the trick.
+ 1
Use isdigit() first to check if the string only contains digits, then convert it to an int:
import math
radius = input("Enter radius: ")
if radius.isdigit():
area= math.pi * int(radius) ** 2
print("The area of your circle is:", area)
else:
print("That is not a valid number")
+ 1
Thank you people for your responses. I appreciate. I have a learnt something from your answers. I also discovered another way to do it.
import math
while True:
try:
radius = int(input("Enter radius"))
break
except:
print("That is not a valid number")
area = math.pi * radius ** 2
print("The area of your circle is: ")
0
Jericho Arcelao Thank you for response. However I stil get the same error when I change input type to int like you suggested. What I am trying to do is to make sure the user inputs an integer otherwise they would get a message telling them the character entered is not a valid number
0
Jan Markus thank you but I am still having the same issue.
0
import math
radius = int(input("Enter radius: "))
if type(radius) is int:
area= math.pi * radius ** 2
print("The area of your circle is:", area)
else:
print("That is not a valid number")
0
Thank you Jericho Arcelao. The isdigit function worked.
0
Fery Ardiansyah thanks for your response. Your code works if user inputs an integer but gives a Traceback error when you enter anything else