Python 'int' object has no attribute 'isdigit'

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

score = int(input("Enter your score: "))

if not score < 0:
    if score.isdigit():
        if score > 100 and score < 0:
            grade = 'Between 0 and 100'
        else:
            grade = 'asd'
    else:
        grade = 'Please enter a number'
else:
    grade = 'negative numbers cannot be entered'

print(grade)

'int' object has no attribute 'isdigit'

There's an error that doesn't match the integer shape. Please tell me where to modify the shape.

python

2022-09-20 08:47

1 Answers

It's probably because isdigit is a command to check strings. If the string is entered in the first place, the int conversion operation results in an error. Try configuring it as follows.

score = input ("Please enter a score"). : ")
try:
    score = int(score)
except:
    Rise Exception ('Please enter a number')

if not score < 0:
    if score > 100 and score < 0:
        grade = '0 to 100.'
else:
    grade = 'negative number entered.'

print(grade)


2022-09-20 08:47

If you have any answers or tips


© 2022 pinfo. All rights reserved.