Megalypse
Megalypse

Reputation: 13

isalpha() and isascii() with parentheses causing errors, but not without

What is the objective of these parentheses and why are the errors occurring ?

This is the error I receive when an ASCII character is typed with the parentheses after a.isalpha and a.isascii:

Traceback (most recent call last): File "C:/---/---/PycharmProjects/PythonExercicios/test.py", line 4, in a1 = float(a) ValueError: could not convert string to float: '!'

a = str(input('Type the "a" side of the triangle: ')).strip()
while a.isascii() and a.isalpha():
    a = input('Please, type again using only numbers: ')
a1 = float(a)

Upvotes: 0

Views: 398

Answers (1)

John Gordon
John Gordon

Reputation: 33335

The while loop exits when an exclamation mark is entered, because it is ascii but it is not alphanumeric.

So then it goes to the next line and float() throws an error because ! cannot be converted to a floating point number.

Perhaps you want this instead?

while not a.isdigit():

Upvotes: 1

Related Questions