+ 1
Help please. Write a program error if the user input is an integer or float, or anything that doesn't consist of words .
the input function turns the user input (whether alphabets or numbers into a string so how do I make this program accepts only string values like Henry,jake or Peter. When I run this program and I enter a number or float the statement in the if block still runs. How do I go about this? name = input('enter your name, alphabets only: ') if type(name)==str: print('welcome {}'.format(name)) else: print('i said alphabets only, what the heck is wrong with you')
19 Answers
+ 3
rodwynnejones what if it's not a number ? You only considered them to be numbers, when then can be any character: %, €, @...
You need to eliminate them all, and leave letters only, and space character
+ 3
I'll need to study more then. Thanks for the answers I appreciate 🙏
+ 1
try this:-
try:
name = input('enter your name, alphabets only: ')
int(name)
print('i said alphabets only, what the heck is wrong with you')
except ValueError:
print('welcome {}'.format(name))
+ 1
Aymane Boukrouh thanks I just looked it up now. Is there any other way around this apart from the one you used?
+ 1
Onimisi depends on what you want to do exactly, but if you want to check that there are alphabets only, I don't know any other way to do it other than check each and every character on its own.
+ 1
Aymane Boukrouh is there any reference material I can read on this subject?
+ 1
this then
import re
name = input('enter your name, alphabets only: ')
if len(re.findall("[^a-zA-Z ]", name)) == 0:
print('welcome {}'.format(name))
else:
print('i said alphabets only, what the heck is wrong with you')
edited:- space after the 'Z' to allow more than one word.
+ 1
Onimisi I don't so, this only uses loops and conditions, there might be some frameworks thought, but I don't know any, I just make a def depending on my needs
+ 1
I suggest you use the regex.
+ 1
swim
I was looking at you code (hope you don't mind) and it got me thinking...."i wonder if........and yes.....you can:-
print("input accepted" if input('Enter you name').isalpha() else "only alphabets allowed")
(although..... it only accepts one word string)
+ 1
Thanks ~ swim ~ 🙏🙇
+ 1
______________________________________
myList = ['1' , '2', '3', '4' ... '9']
for number in myList:
if number not in name:
print()
else:
print("error")
_______________________________________
Ok, this program will solve your problem because floats and intgers bouth contain numbers in string type and, you can add what ever item you want to your list to give error for user if the user enters that item of list.
0
Hey, you could do something like this
name = str(input ('Digit your name :'))
If name.isalpha():
Print('Hello ...')
Else:
Print('...')
The method isalpha() is a string method that return true if all items in the string are in alphabet, so if the user input some number or any special characters, it will return false and will run what's in the else statement
Good studies man 🙏
0
Use regular expressions:
import re
pattern = r"[^a-zA-Z]"
if re.search(pattern, some_input):
print("Your input sucks")