我如何修正生成随机骰子点数的程序

huangapple go评论72阅读模式
英文:

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中对于代码块非常重要。似乎在行17val=random.randint(1,7))和21counter=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(&quot;get out!&quot;)
  quit()
import random
print(&quot;chooses random dice rolls&quot;)
maximumval=int(input(&quot;how many dice rolls do you want?&quot;))
if maximumval&lt;0:
   reject()
else:
    counter=1
    while counter&lt;=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.

huangapple
  • 本文由 发表于 2023年5月18日 08:08:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定