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. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

10th May 2018, 11:48 AM
IKECHUKWU, GAVIN C
IKECHUKWU, GAVIN C - avatar
8 Answers
+ 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.
10th May 2018, 11:52 AM
apex137
apex137 - avatar
+ 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")
10th May 2018, 12:17 PM
apex137
apex137 - avatar
+ 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: ")
10th May 2018, 12:42 PM
IKECHUKWU, GAVIN C
IKECHUKWU, GAVIN C - avatar
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
10th May 2018, 12:09 PM
IKECHUKWU, GAVIN C
IKECHUKWU, GAVIN C - avatar
0
Jan Markus thank you but I am still having the same issue.
10th May 2018, 12:10 PM
IKECHUKWU, GAVIN C
IKECHUKWU, GAVIN C - avatar
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")
10th May 2018, 12:27 PM
Fery Ardiansyah
Fery Ardiansyah - avatar
0
Thank you Jericho Arcelao. The isdigit function worked.
10th May 2018, 12:33 PM
IKECHUKWU, GAVIN C
IKECHUKWU, GAVIN C - avatar
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
10th May 2018, 12:38 PM
IKECHUKWU, GAVIN C
IKECHUKWU, GAVIN C - avatar