英文:
How can I correct the program which generates random dice rolls
问题
代码中的问题在于第17行的缩进不正确。请将该行的缩进修正为与其上一行相同,像这样:
counter+=1
这样,代码应该能够正常运行,而不会再出现 parseError。
英文:
def reject():
print("get out!")
quit()
import random
print("chooses random dice rolls")
maximumval=int(input("how many dice rolls do you want?"))
if maximumval<0:
reject()
else:
counter=1
while counter<=maximumval:
counter=str(counter)
mydict={
}
val=random.randint(1,7)
val=str(val)
mydict.update({counter:val})
val=int(val)
counter=int(counter)
counter+=1
print(mydict)
it gives parseError on line 17
I was not able to solve it. It says parseError.
答案1
得分: 0
缩进在Python中对于代码块非常重要。似乎在行17
(val=random.randint(1,7)
)和21
(counter=int(counter)
)之间的所有行,包括两端,都有额外的空格。删除额外的空格,你的代码就应该可以正常运行了。
修复后的代码:
def reject():
print("get out!")
quit()
import random
print("chooses random dice rolls")
maximumval=int(input("how many dice rolls do you want?"))
if maximumval<0:
reject()
else:
counter=1
while counter<=maximumval:
counter=str(counter)
mydict={
}
val=random.randint(1,7)
val=str(val)
mydict.update({counter:val})
val=int(val)
counter=int(counter)
counter+=1
print(mydict)
考虑使用带有正确扩展的IDE/文本编辑器,这将有助于识别此类错误。
英文:
Indentation in python is crucial for code blocks. It seems like you have extra spaces in front of all rows between row 17
( val=random.randint(1,7)
) and 21
( counter=int(counter)
) including both edges. Remove the extra spaces and you should be good to go.
Fixed code:
def reject():
print("get out!")
quit()
import random
print("chooses random dice rolls")
maximumval=int(input("how many dice rolls do you want?"))
if maximumval<0:
reject()
else:
counter=1
while counter<=maximumval:
counter=str(counter)
mydict={
}
val=random.randint(1,7)
val=str(val)
mydict.update({counter:val})
val=int(val)
counter=int(counter)
counter+=1
print(mydict)
Consider using an IDE/Text Editor like vscode with the correct extensions this will help you identify such mistakes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论