提问人:el smooth 提问时间:12/4/2022 更新时间:12/4/2022 访问量:50
SyntaxError:解析 -Python 时出现意外的 EOF
SyntaxError: unexpected EOF while parsing -Python
问:
我正在使用优先级调度的概念来创建一个程序,该程序应该接受两个输入、传入数据包的数量和传入数据包的优先级字符串。例如,对于输入“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()
任何帮助都是值得赞赏的,谢谢
答: 暂无答案
评论
input
main
import sys
print(sys.version)
print(sys.version)