+ 2
Python Help me pleases
I can't write a code which takes only number and if the entered is string then my program should say 'enter a num'
13 odpowiedzi
+ 7
Hello, 😊
If you need help, you can post the code you're struggling with!
• SEARCH for similar QUESTIONS or ANSWERS before posting
• Include relevant TAGS
Do not ask someone else to do it for you!!?
• https://www.sololearn.com/post/75089/?ref=app
• https://code.sololearn.com/WvG0MJq2dQ6y/
+ 4
Use try-except to prompt an error msg that the user can understand
try:
x = int(input(str("Enter a number: ")))
print(x)
except ValueError:
print("!!! INPUT SHOULD BE NUMBER")
except NameError:
print("!!!")
+ 3
number = input(":")
if(number.isnumeric()):
print(number)
else:
print("Enter a Num")
But a while loop should be used instead only that it will not work in sololearn
+ 1
i know guys it a shame of me cuz i have forgetten it and it my fault that i didnt write any program for 6 month, now i dont remember anything 😭😭😔but now i needed it therewhy asked for a help
+ 1
Here you go:
#write it as it is
notnum = True
while notnum:
if bool(input("enter number: ").isdigit()) is False:
print("please enter a number")
else:
print("number accepted")
notnum = False
#to force user to input just number. and if you dont need while just remove it and remove notnum
0
There is a string method isnumeric.
"56".isnumeric() -> True
"fiftysix".isnumeric() -> False
It does not handle negative integers or decimals:
"-56".isnumeric() -> False
"5.6".isnumeric() -> False
"fifty6".isnumeric() -> False
0
if not input().isnumeric():
print("enter a num")
- 2
guys could you just write a program if it, conditions dont work
- 2
i'm going to revise it of course but i needed it just right now so im just trying to write it coz dont have time to revise, i need to make it today