[英]Keep receiving error- builtins.AttributeError: 'int' object has no attribute 'isdigit'
First part is working fine but Whenever i enter name of the file in grades = input() i receive AttributeError. 第一部分工作正常,但是每当我以等级= 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: ')
You can really simplify this code: 您可以真正简化此代码:
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: ')
为什么不使用简单的强制转换来确保输出的类型始终是str?
option = str(input('Enter your option: '))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.