gpt4 book ai didi

python - 编写一个简单的 "Rock Paper Scissors"游戏机器人

转载 作者:太空狗 更新时间:2023-10-29 22:00:15 26 4
gpt4 key购买 nike

这就是我为我的剪刀石头布游戏想出的:

import random 

from time import sleep

print "Please select: "
print "1 Rock"
print "2 Paper"
print "3 Scissors"

player = input ("Choose from 1-3: ")

if player == 1:
print "You choose Rock"
sleep (2)
print "CPU chooses Paper"
sleep (.5)
print "You lose, and you will never win!"

elif player == 2:
print "You choose Paper"
sleep (2)
print "CPU chooses Scissors"
sleep (.5)
print "You lose, and you will never win!"

else:
print "You choose Scissors"
sleep (2)
print "CPU chooses Rock"
sleep (.5)
print "You lose, and you will never win!"

我想让程序做的是无论用户输入什么,都从三个选项(剪刀石头布)中随机选择 1 个。我怎样才能做到这一点?

最佳答案

好吧,您已经导入了随机模块,这是一个开始。

试试 random.choice 函数。

>>> from random import choice
>>> cpu_choice = choice(('rock', 'paper', 'scissors'))

关于python - 编写一个简单的 "Rock Paper Scissors"游戏机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/453396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com