+ 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'

4th Oct 2019, 6:58 AM
Huan
Huan - avatar
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/
4th Oct 2019, 7:29 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 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("!!!")
4th Oct 2019, 8:17 AM
Tochukwu 🇳🇬
Tochukwu 🇳🇬 - avatar
+ 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
4th Oct 2019, 7:20 AM
Qudusayo
Qudusayo - avatar
+ 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
4th Oct 2019, 7:11 AM
Huan
Huan - avatar
+ 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
4th Oct 2019, 8:58 PM
William M
William M - avatar
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
4th Oct 2019, 7:04 AM
Seb TheS
Seb TheS - avatar
0
if not input().isnumeric(): print("enter a num")
4th Oct 2019, 7:08 AM
Seb TheS
Seb TheS - avatar
- 2
guys could you just write a program if it, conditions dont work
4th Oct 2019, 7:07 AM
Huan
Huan - avatar
- 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
4th Oct 2019, 7:15 AM
Huan
Huan - avatar