继续接收error- builtins.AttributeError:' int'对象没有属性' isdigit'

时间:2017-10-11 05:04:29

标签: python

第一部分工作正常但每当我在grade = input()中输入文件的名称时,我会收到AttributeError。

print('Choose one of the following options:')
print('1. Calculate the average grade for each student.')
print('2. Print the highest or lowest scores based on the user input.')
print('3. Find the average score of the entire class (all students) in the 
final exam.')
print('4. To quit')

option = input('Enter your option: ')
while True:



    if not option.isdigit() or not int(option)>0 :
        print('Please enter a valid option from 1 to 4')
        option = input('Enter your option: ')
        continue

    option = int(option)
    if option in [1,2,3,4] :
        grades = input('Enter the name of the file')

    else: 
        print('Please enter a valid option from 1 to 4')
        option = input('Enter your option: ')

2 个答案:

答案 0 :(得分:1)

您可以真正简化此代码:

print('Choose one of the following options:')
print('1. Calculate the average grade for each student.')
print('2. Print the highest or lowest scores based on the user input.')
print('3. Find the average score of the entire class (all students) in the final exam.')
print('4. To quit')

option = input('Enter your option: ')

while True:
    if option in ['1', '2', '3', '4']:
        option = int(option)
        grades = input('Enter the name of the file')
    else: 
        print('Please enter a valid option from 1 to 4')
        option = input('Enter your option: ')

答案 1 :(得分:0)

为什么不使用简单的强制转换来确保输出类型始终是str?

option = str(input('Enter your option: '))