0
How does isalpha() work with objects?
try: p = Person(input(âname: â),int(input(âage: )),input(âsex: â)) assert p.name.isalpha() and p.sex.isalpha() except: print(âwrong, try againâ) This wont work
3 Answers
+ 6
Lenoname ,
we have already been discussing this issue earlier today, so this is a duplicate of: https://www.sololearn.com/Discuss/2990339/?ref=app
we are missing some parts of your code (i assume that *Person* is a class, that has 3 attributs, and *p* is an instance of class Person. so *p.name* returns a string from instance attribut name ... )
but here is a simple answer:
<string>.isalpha() is a python string method that returns True, if all characters of the given string are letters.
"Hello".isalpha() => True
"hello world".isalpha() => False, since there is a whitespace in the string
"hello1".isalpha() => False, since there is a digit in the string
+ 5
Lenoname ,
yes, you can write it like you mentioned it and how it is shown in this code:
try:
p = Person(input("name: "),int(input("age: ")),input("sex: "))
assert p.name.isalpha() and p.sex.isalpha()
print("name and sex are strings")
except:
print("wrong, try again")
this code is reworked because wrong quotes for the strings are used. there is also a mix of tabs and spaces fod indentation in your 2 code parts.
https://code.sololearn.com/cE0bae4wNFwd/?ref=app
0
Lothar The missing parts are:
class Person:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def __str__(self):
return f'{self.name},{self.age},{self.sex}'
def __repr__(self):
return f'{self.name},{self.age},{self.sex}'
I know about that, but how do i check wether p.name is alpha?can i even write it like that?: p.name.isalpha()