我得到一个错误“必须是str,而不是内置的函数或方法”

2024-10-03 19:32:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个密码

 strMph = ""
 numberplate = ""
 print("Today we are going to determine how fast vehicles are going.")
 while numberplate != "-1":
 numberplate = str(input("Enter a numberplate or enter -1 to stop and show all vehicles that went over the speed limit "))
if numberplate != "-1":
    name = str(input("Please enter the name of the owner of that car "))
    adress = str(input("Please enter the adress of the owner of that car "))
    distance = 30
    time = float(input("How long did that take in seconds to travel between the two speed cameras that are 30 meters apart "))
    speed = distance / time
    mps = str(speed)
    strMph = speed * 2.23
    mph = str(strMph)
    print("That is a speed of " +mps +" meters per second or  " + mph+ " miles per hour ")
    if strMph > 40:
            valid = False
            if (numberplate.count(numberplate.isalpha) == 5):
                if (numberplate.count(int) == 2):
                    valid = True
            else:
                valid == False

            if valid == True:
                print("This numberplate is  a standard numberplate")
                file = open("IllegalPlates.txt", "a")
                file.write("Numberplate")
                file.write(numberplate)
                file.write("Is a standard numberplate")
                file.write("and belongs to")
                file.write(name)
                file.write("who lives at")
                file.write(adress)
                file.write(",")
                file.close()
            else:
                print("This numberplate is not a standard numberplate")
                file = open("IllegalPlates.txt", "a")
                file.write("Numberplate")
                file.write(numberplate)
                file.write("Is not a standard numberplate")
                file.write("and belongs to")
                file.write(name)
                file.write("who lives at")
                file.write(adress)
                file.write(",")
                file.close()
else:
    print ("The cars that will be reciving a ticket are ")
    file = open("IllegalPlates.txt", "r")
    print (file.readlines())
    f = open("IllegalPlates.txt", 'w')
    break

我得到了这个错误信息

^{pr2}$

不起作用的是当我输入一个列表时,我所使用的验证车牌号的方法干扰了这一点。有人能帮忙吗?另外,我使用的是python3.6.0,我知道代码很混乱,我对此有点陌生。缩进可能不好,但我的代码中没有。建议会很好。在


Tags: ofthetonameinputifthatare
2条回答

这一行就是产生错误的那一行:

if (numberplate.count(numberplate.isalpha) == 5)

isalpha是一个函数,因此应该在它后面加上()。但是,它仍然没有多大意义,因为它返回一个布尔值。 这个也很奇怪:

^{pr2}$

你到底想达到什么目的?在

你想在这里做什么?该代码被构建为一个接一个地接受输入。你是说你想一次性输入多个值?在

相关问题 更多 >