如何用PYTHON制作剪刀石头布游戏

2025-05-26 20:29:21

1、打开PYTHON,新建一个空白的PYTHON文档。

如何用PYTHON制作剪刀石头布游戏

3、player1 = input("Please select your way to win: ")player2 = input("Please select your way to win: ")if player1 == "rock" and player2 == "scissors": print("The winner is player1")elif player1 == "scissors" and player2 == "paper": print("The winner is player1")elif player1 == "paper" and player2 == "rock": print("The winner is player1")换了另一个输入就可以看出有问题了。

如何用PYTHON制作剪刀石头布游戏

5、rules = {"rock": {"scissors": "wins", "paper": "loses"}, "scissors": {"rock": "loses", "paper": "wins"}, "paper": {"rock": "wins", "scissors": "loses"}}player1 = input("Please select your way to win: ")player2 = input("Please select your way to win: ")print("The player1 " + rules[player1][player2])我们也可以用字典来定义规则,字典里面还可以添加字典,这样直接导出结果。

如何用PYTHON制作剪刀石头布游戏

7、import randomplayer1 = input("Please select your way to win: ")def who_wins(choice): options = ["rock", "scissors", "paper"] player2 = random.choice(options) print("player2 is using {}.".format(player2)) if player1 == "rock": if player2 == "scissors": print("The winner is player1") else: print("The winner is player2") elif player1 == "scissors": if player2 == "rock": print("The winner is player2") else: print("The winner is player1") elif player1 == "paper": if player2 == "rock": print("The winner is player1") else: print("The winner is player2") elif player1 == player2: return("Draw") who_wins(player1)我们还可以用RANDOM来随机玩家输入的选项,这样会更加有趣。

如何用PYTHON制作剪刀石头布游戏
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢