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

24th Sep 2019, 5:44 PM
Onimisi
Onimisi - avatar
19 odpowiedzi
24th Sep 2019, 6:16 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 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
24th Sep 2019, 6:42 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
I'll need to study more then. Thanks for the answers I appreciate 🙏
24th Sep 2019, 7:11 PM
Onimisi
Onimisi - avatar
+ 3
~ swim ~ I'm already half way through my course in python but I don't recognize any of the function you used . What section is the isaalpha part in?
24th Sep 2019, 9:31 PM
Onimisi
Onimisi - avatar
+ 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))
24th Sep 2019, 6:41 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Aymane Boukrouh thanks I just looked it up now. Is there any other way around this apart from the one you used?
24th Sep 2019, 6:56 PM
Onimisi
Onimisi - avatar
+ 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.
24th Sep 2019, 6:59 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
Aymane Boukrouh is there any reference material I can read on this subject?
24th Sep 2019, 7:06 PM
Onimisi
Onimisi - avatar
+ 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.
24th Sep 2019, 7:07 PM
rodwynnejones
rodwynnejones - avatar
+ 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
24th Sep 2019, 7:09 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
I suggest you use the regex.
24th Sep 2019, 7:29 PM
LODEKI BRIAN
LODEKI BRIAN - avatar
+ 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)
24th Sep 2019, 9:08 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Thanks ~ swim ~ 🙏🙇
24th Sep 2019, 9:44 PM
Onimisi
Onimisi - avatar
+ 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.
27th Sep 2019, 1:31 PM
parsa akbari
parsa akbari - avatar
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 🙏
26th Sep 2019, 12:39 AM
Michael Toninger
0
Use regular expressions: import re pattern = r"[^a-zA-Z]" if re.search(pattern, some_input): print("Your input sucks")
26th Sep 2019, 4:50 PM
Evgeniy Yunkin
Evgeniy Yunkin - avatar