+ 4
How To Check If User Input is a Number or Not
I have a program that checks whether the user input is greater than 8. I want it to have a response for every situation, even if the user enters in words. How to I check whether their input is words and then change it to another number, like 14 so it can go through the rest of the program? https://code.sololearn.com/c4V09YHZb4ng/?ref=app
4 ответов
+ 11
n = input("Enter a number: ")
if not n.isdigit():
print("You didn't enter a number!")
+ 1
import java.util.Scanner;
public class Numb{
public static void main(String args[]){
boolean cond=true; string i=“”;
Exception e2=null;
while(cond){
try{
System.out.printIn(“Enter Digits ONLY”);
i=new Scanner(System.in).next();
for(int a=0; a<i.length();++a)
if(!Charater.isDigit(i.charAt(a)))throw e2;;
cond=false;
}
catch(Exception e){
System.out.printIn(“Numbers only,please”);
}}
System.out.printIn(“following is only numbers\n”+i);
}}
// this is another approach
0
Flamibat ...n=int(input(" enter a number"))
if n.isdigit()==True:
Print("number")
- 1
3
nmb