简体   繁体   English

持续收到错误-builtins.AttributeError:“ int”对象没有属性“ isdigit”

[英]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.

相关问题 builtins.AttributeError: 'int' object 没有属性 'get_ticks' - builtins.AttributeError: 'int' object has no attribute 'get_ticks' 接收到错误的buildins.AttributeError:'pygame.font.Font'对象没有字体的'SysFont'属性,为什么不知道为什么? - Receiving error builtins.AttributeError: 'pygame.font.Font' object has no attribute 'SysFont' for font and can't figure out why? Python /烧瓶条纹错误:builtins.AttributeError AttributeError:类型对象“ Subscription”没有属性“ create” - Python/Flask Stripe Error: builtins.AttributeError AttributeError: type object 'Subscription' has no attribute 'create' Builtins.AttributeError:'NoneType'对象没有属性'check_password' - builtins.AttributeError: 'NoneType' object has no attribute 'check_password' builtins.AttributeError: 'module' 对象没有属性 'one_tile' - builtins.AttributeError: 'module' object has no attribute 'one_tile' pybacktest 库 hello world 错误:builtins.AttributeError: 'Series' object 没有属性 'ix' - pybacktest library hello world error: builtins.AttributeError: 'Series' object has no attribute 'ix' builtins.AttributeError AttributeError:模块“ csv”没有属性“ Dictwriter” - builtins.AttributeError AttributeError: module 'csv' has no attribute 'Dictwriter' AttributeError:'int'对象没有属性'isdigit' - AttributeError: 'int' object has no attribute 'isdigit' AttributeError: 'int' 对象没有属性 'isdigit' 和 NameError - AttributeError: 'int' object has no attribute 'isdigit' and NameError 我该如何解决? Builtins.AttributeError:'NoneType'对象没有属性'_helper' - How would I fix this? builtins.AttributeError: 'NoneType' object has no attribute '_helper'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM