SyntaxError:解析 -Python 时出现意外的 EOF

SyntaxError: unexpected EOF while parsing -Python

提问人:el smooth 提问时间:12/4/2022 更新时间:12/4/2022 访问量:50

问:

我正在使用优先级调度的概念来创建一个程序,该程序应该接受两个输入、传入数据包的数量和传入数据包的优先级字符串。例如,对于输入“4”和“HLHH”,传出数据包的顺序应为“0 2 1 3”。

当我运行程序时,我不断收到此错误:

Input:4 HLHH
Traceback (most recent call last):
  File "schedule.py", line 71, in <module>
    main()
  File "schedule.py", line 62, in main
    inputStr=input("Input:")
  File "<string>", line 1
    4 HLHH
         ^
SyntaxError: unexpected EOF while parsing

这是我的源代码:

import re

#Used to set the priorities for initial buffer
def setPriorityForBuffer(priorityStr,priorityList):
    counter=0
    outgoingPkt=[]
    #Set priority for H first
    for i in range(0,3):
        if priorityStr[i]=='H':
            outgoingPkt.append(priorityList[counter])
            counter+=1
        else:
            outgoingPkt.append('NA')
    #Set priority for L next
    for i in range(0,3):
        if outgoingPkt[i]=='NA':
            outgoingPkt[i]=priorityList[counter]
            counter+=1
    return outgoingPkt

def getPriority(numPackets,priorityStr):
    outputStr = " " 
    outgoingPkt=[]
    priorityList=[]
    for i in range(0,numPackets):
        priorityList.append(str(i))
    if numPackets<=0:
        return 'Wrong input: the number of packets must be greater than 0.'
    elif numPackets!=len(priorityStr):
        return 'Wrong input: the number of packets is wrong.'
    else:
        pattern = re.compile('^[LH]+$')
        if not re.search(pattern, priorityStr):
            return 'Wrong input: the priority must be H or L'
        if numPackets==3:
            outgoingPkt=setPriorityForBuffer(priorityStr,priorityList)
        elif numPackets>3:
            #For first 3 packets
            outgoingPkt=setPriorityForBuffer(priorityStr,priorityList)
            #For remaining packets
            counter=3
            #Set priority for H first
            for i in range(3,len(priorityStr)):
                if priorityStr[i]=='H':
                    outgoingPkt.append(priorityList[counter])
                    counter+=1
                else:
                    outgoingPkt.append('NA')
            #Set priority for L next
            for i in range(3,len(priorityStr)):
                if outgoingPkt[i]=='NA':
                    outgoingPkt[i]=priorityList[counter]
                    counter+=1
                    
        for i in outgoingPkt:
            outputStr=outputStr+i+" "
            
    return outputStr
                    
def main():
    #Get input from user
    inputStr=input("Input:")
    inputList=inputStr.split(" ")
    numPackets=int(inputList[0])
    priorityStr=inputList[1]
    #Call function and print outgoingpacket string
    print("Output:",getPriority(numPackets,priorityStr))
    

#Driver code
main()
    

任何帮助都是值得赞赏的,谢谢

Python EOF

评论

0赞 tdelaney 12/4/2022
你用的是哪条蟒蛇?这看起来像旧 python 2.x 中试图评估输入的函数。input
0赞 tdelaney 12/4/2022
作为在 stackoverflow 上调试和提问的说明,一旦出现错误提示,后面的代码就无关紧要了。您的问题出在执行的第一行中。只需几行代码即可重现此内容。这使每个人都更容易看到正在发生的事情。
0赞 tdelaney 12/4/2022
就在打电话之前,你可以然后.mainimport sysprint(sys.version)
0赞 el smooth 12/4/2022
您好,我正在使用 python 3.10
0赞 tdelaney 12/4/2022
返回的确切字符串是什么?print(sys.version)

答: 暂无答案