我正在尝试构建一个石头、纸、剪刀游戏。我似乎无法让代码进行锻炼,因为无论我做出什么选择,赢家总是电脑!我的电脑有偏见吗?感谢您的帮助 !import randomcomp_list = ['Rock', 'Paper', 'Scissors']computer = c = 0command = p = 0print("Scores: Computer " + str(c) + " player " + str(p))while True: comp = random.choice(comp_list) command = input("rock , paper, scissors or quit :").lower() if command == comp : print("break even") elif command == "rock": if comp == "scissors": print("player won") p += 1 else: print("computer won") c +=1 elif command == "paper": if comp == "rock": print("player won") p += 1 else : print("computer won") c += 1 elif command == "scissors": if comp == "paper": print("player won") p += 1 else : print("computer won") c += 1 elif command == "quit": break else : print("wrong command! ") print("Player: " + command) print("computer: " + comp) print("") print("Scores: Computer " + str(c) + " player " + str(p)) print("")
1 回答
蝴蝶刀刀
TA贡献1801条经验 获得超8个赞
您的计算机总是生成一个大写字符串 ex
Rock
你的输入总是小写的
rock
在 python 字符串情况下很重要,所以当你比较时
if command == comp:
这是比较
"Rock" == "rock" # -> which is false
您可以通过对字符串使用全部小写来修复您的代码。只需将顶部的数组更改为全部小写即可
['rock', 'paper', 'scissors']
添加回答
举报
0/150
提交
取消