用python写一个石头剪刀布的游戏
用python写一个石头剪刀布的游戏 好的,下面是一个简单的石头剪刀布游戏的Python代码:import random
def game():
choices = ['石头', '剪刀', '布']
computer_choice = random.choice(choices)
user_choice = input("请输入你的选择(石头/剪刀/布):")
if user_choice not in choices:
print("输入无效,请重新输入")
return
print("你选择了:" + user_choice)
print("计算机选择了:" + computer_choice)
if user_choice == computer_choice:
print("平局!")
elif (user_choice == '石头' and computer_choice == '剪刀') or (user_choice == '剪刀' and computer_choice == '布') or (user_choice == '布' and computer_choice == '石头'):
print("恭喜你,你赢了!")
else:
print("很遗憾,你输了。")
game()
在这个代码中,我们首先定义了一个`game`函数来表示游戏的逻辑。在每一局游戏开始时,计算机会随机选择石头、剪刀或布,并等待用户输入他们的选择。然后根据用户和计算机的选择进行比较,并输出胜负结果。
希望这个简单的代码能帮助你入门Python编程,享受游戏的乐趣!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 import random
man_choice = input("请输入你的选择(石头/剪刀/布):")
if man_choice == "石头":
man=1
elif man_choice == "剪刀":
man=2
elif man_choice == "布":
man=3
choices = ['石头', '剪刀', '布']
computer_choice=random.choice(choices)
for choice in computer_choice:
if choice == "石头":
computer=1
elif choice == "剪刀":
computer=2
elif choice == "布":
computer=3
print("电脑出的是:",computer_choice)
##石头1 剪刀2 布3
#12 13 21 23 31 32
if computer==1 and man==2:
print("电脑赢")
elif computer==1 and man==3:
print("我胜")
elif computer==2 and man==1:
print("我胜")
elif computer==2 and man==3:
print("电脑赢")
elif computer==3 and man==1:
print("电脑赢")
elif computer==3 and man==2:
print("我胜")
else:
print("平局")
哪里错了?要怎么改? chen` 发表于 2024-04-08 09:46
import random
man_choice = input("请输入你的 ...
代码中有几个问题需要修正:
1. 在定义变量`choices`和`computer_choice`时,没有为`choices`赋值一个选项列表。你可以将它设置为`choices = ["石头", "剪刀", "布"]`。
2. 在for循环中,你不需要对`computer_choice`进行迭代,因为它只是一个字符串。你只需要根据`computer_choice`的值来确定`computer`的值即可。
3. 在判断胜负时,你需要考虑到平局的情况。可以先判断是否平局,再判断其他胜负情况。
以下是修正后的代码:
import random
choices = ["石头", "剪刀", "布"]
man_choice = input("请输入你的选择(石头/剪刀/布):")
if man_choice == "石头":
man = 1
elif man_choice == "剪刀":
man = 2
elif man_choice == "布":
man = 3
computer_choice = random.choice(choices)
if computer_choice == "石头":
computer = 1
elif computer_choice == "剪刀":
computer = 2
elif computer_choice == "布":
computer = 3
print("电脑出的是:", computer_choice)
if computer == man:
print("平局")
elif (computer == 1 and man == 2) or (computer == 2 and man == 3) or (computer == 3 and man == 1):
print("电脑赢")
else:
print("我胜")
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 面向对象:
class Player:
def game2(self):
score = 0# 积分
times = 0# 局数
import random
print('平局+0分,败局-1分,胜局+1分')
print('3局过后谁会赢呢?')
while times < 3:
computer = random.randint(1, 3)
n = input('1代表石头,2代表剪刀,3代表布 出一个吧:')
try:
n = int(n)
except ValueError as e:
print('您输入的不是1~3中的整数!')
continue
right = (1, 2, 3)
while n not in right:
if n not in right:
print('您输入的不是1~3中的整数!')
n = int(input('1代表石头,2代表剪刀,3代表布 出一个吧:'))
continue
try:
n = int(n)
except ValueError as a:
print('您输入的不是1~3中的整数!')
continue
else:
n = int(n)
if computer == 1:
if n == 1:
times += 1
score += 0
print('程序和你都出石头,平局+0分','当前第%d局,当前积分%d'%(times,score),sep='\n')
if n == 2:
times += 1
score -= 1
print('程序出石头,你出的是剪刀,败局-1分','当前第%d局,当前积分%d'%(times,score),sep='\n')
if n == 3:
times += 1
score += 1
print('程序出石头,你出的是布,胜局+1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')
elif computer == 2:
if n == 1:
times += 1
score += 1
print('程序出剪刀,你出石头,胜局+1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')
if n == 2:
times += 1
score += 0
print('程序和你都出剪刀,平局+0分', '当前第%d局,当前积分%d' % (times, score), sep='\n')
if n == 3:
times += 1
score -= 1
print('程序出剪刀,你出的是布,败局-1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')
else:
if n == 1:
times += 1
score -= 1
print('程序出布,你出石头,败局-1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')
if n == 2:
times += 1
score += 1
print('程序出布,你出剪刀,胜局+1分', '当前第%d局,当前积分%d' % (times, score), sep='\n')
if n == 3:
times += 1
score += 0
print('程序和你都出布,平局+0分', '当前第%d局,当前积分%d' % (times, score), sep='\n')
if times < 3:
print('开始下一局')
if score >= 1:
print('\n','你赢了!')
elif score == 0:
print('\n','你与程序平手')
else:
print('\n','你输了!真给人类丢脸')
player = Player()
player.game2() 某一个“天” 发表于 2024-8-3 13:05
面向对象:
大佬,膜拜一下,带小弟么{:5_110:} Stubborn 发表于 2024-8-7 17:48
大佬,膜拜一下,带小弟么
{:10_256:}带 某一个“天” 发表于 2024-8-7 18:21
带
加个联系方式,请教下五子棋的局面评估函数怎么写{:5_101:} Stubborn 发表于 2024-8-8 13:16
加个联系方式,请教下五子棋的局面评估函数怎么写
这个可不简单{:10_256:} 某一个“天” 发表于 2024-8-8 15:16
这个可不简单
你可以的{:10_254:} 本帖最后由 Stubborn 于 2024-8-8 15:40 编辑
import random
class Player:
@staticmethod
def game2():
print('平局+0分,败局-1分,胜局+1分。\n3局过后谁会赢呢?')
time,score = 3,0
while True and time:
try:
n = int(input('1代表石头,2代表剪刀,3代表布 出一个吧:'))
if 0 > n or n > 3:
continue
except:
print('您输入的不是1~3中的整数!')
continue
computer = random.randint(1, 3)
def compare(n, c):
if in (, , ):
return 1
if in (, , ):
return 0
return -1
r = compare(n, computer)
rl = ['石头','剪刀','布']
el = ['平局','你赢了','你输了']
print('程序出{},你出的是{},{}{}分, 当前第{}局'.format(rl,rl,el, r, abs(time-3)) )
time -= 1
score+= r
print('开始下一局')
Player.game2()
Stubborn 发表于 2024-8-8 15:34
我还真有一个五子棋案例。。。花钱买的 Stubborn 发表于 2024-8-8 15:34
你可以的
你发个求助帖,我把五子棋算法给你{:10_256:} 本帖最后由 Stubborn 于 2024-8-8 16:01 编辑
某一个“天” 发表于 2024-8-8 15:57
你发个求助帖,我把五子棋算法给你
{:10_297:},发了求助铁子
页:
[1]