创建一个命令行游戏,游戏者可以在石头、剪刀和布之间进行选择,与计算机PK。如果游戏者赢了,得分就会添加,直到结束游戏时,最终的分数会展示给游戏者。
大哥 网上源码一大堆
不知道你这个问题是否已经解决, 如果还没有解决的话:import random
score = 0
while True:
# 电脑随机选择
com = random.choice(["石头", "剪刀", "布"])
# 玩家输入选择
usr = input("请输入石头、剪刀或布:")
# 判断输入是否合法
if usr != "石头" and usr != "剪刀" and usr != "布":
print("输入不合法,请重新输入")
continue
# 比较选择并计算得分
if com == usr:
print("平局,电脑也出了%s" % com)
elif com == "石头" and usr == "剪刀" or com == "剪刀" and usr == "布" or com == "布" and usr == "石头":
print("你输了,电脑出了%s" % com)
score -= 1
else:
print("你赢了,电脑出了%s" % com)
score += 1
# 展示得分并询问是否继续
print("当前得分为%s" % score)
choice = input("是否继续?(y/n)")
if choice == "n":
break
print("游戏结束,最终得分为%s" % score)