所以我有一个课堂作业,我要做一个石头-布-剪刀的游戏,停止作弊。我一直在输入错误:不可损坏的类型:“list”
我不知道是什么原因造成的,有人能帮我解决这个问题吗?在
import random
import re
def MatchAssess():
if userThrow == compThrow:
print("Draw")
elif userThrow == "r" and compThrow == "p":
print("Computer chose paper; you chose rock - you lose")
elif userThrow == "p" and compThrow == "s":
print("Computer chose scissors; you chose paper - you lose!")
elif userThrow == "r" and compThrow == "p":
print("Computer chose paper; you chose rock - you lose!")
elif userThrow == "s" and compThrow == "r":
print("Computer chose rock; you chose scissors - you lose!")
else:
print("you win")
CompThrowSelection = ["r","p","s"]
ThrowRule = "[a-z]"
while True:
compThrow = random.choice(CompThrowSelection)
userThrow = input("Enter Rock [r] Paper [p] or Scissors [s]")
if not re.match(CompThrowSelection,userThrow) and len(userThrow) > 1:
MatchAssess()
else:
print("incorrect letter")
userThrow = input("Enter Rock [r] Paper [p] or Scissors [s]")
应更正为
以及
^{pr2}$您可以这样实现:
我注意到你的逻辑有点错误。在
一个是
re.match()
将应用于模式而不是列表上。对于list
我们可以使用类似于下一个问题是,如果用户输入了一个有效的输入,}。在
len(userThrow) > 1
将永远不会得到满足。所以做len(userThrow) >= 1
甚至{最后,我在条件分支上添加了一个
continue
语句,用于捕捉错误的输入,而不是从那里再次读取输入。在最后,这是工作代码!在
^{pr2}$希望这有帮助!:)
相关问题 更多 >
编程相关推荐