返回

python石头剪刀布和猜数字游戏循环嵌套练习

发布时间:2023-01-01 13:15:37 243
# python

前言:

编程中经典游戏练习-石头剪刀布,训练循环嵌套技巧。

程序的组织结构,顺序、循环、分支。

程序编写方法:IPO、流程图、伪代码。

python石头剪刀布和猜数字游戏循环嵌套练习_循环嵌套

python石头剪刀布和猜数字游戏循环嵌套练习_伪代码_02


# coding:utf-8
import random
player_score = 0
computer_score = 0
print('''
* * * * * * * 欢迎来到4399游戏平台 * * * * * * *
石头 剪刀 布
* * * * * * * * * * * * * * * * * * * * * * * *
''')
player_name = input('请输入玩家姓名:')
print('1、貂蝉 2、曹操 3、诸葛亮')
choice = eval(input('请选择电脑角色:'))
if choice == 1:
computer_name = "貂蝉"
elif choice == 2:
computer_name = "曹操"
elif choice == 3:
computer_name = "诸葛亮"
else:
computer_name = "匿名"
print(player_name, 'VS', computer_name)
while True:
player_fist = eval(input('--------请出拳:1、石头 2、剪刀 3、布--------\n'))
if player_fist ==1:
player_fist_name = "石头"
elif player_fist ==2:
player_fist_name = "剪刀"
elif player_fist ==3:
player_fist_name = "布"
else:
player_fist_name = "石头"
player_fist =1
computer_first = random.randint(1, 3)
if computer_first ==1:
computer_first_name = "石头"
elif computer_first ==2:
computer_first_name = "剪刀"
else:
computer_first_name = "布"
print(player_name,"出拳:", player_fist_name)
print(computer_name, "出拳:", computer_first_name)
if player_fist == computer_first:
print('平局')
elif (player_fist==1 and computer_first==2) or (player_fist==2 and computer_first==3) or (player_fist==3 and computer_first==1):
print('玩家:',player_name,'胜')
player_score +=1
else:
print('电脑:',computer_name,'胜')
computer_score +=1
answer = input('再来一局吗?y/n')
if answer =='n':
break
print('-------------------')
print(player_name,player_score)
print(computer_name,computer_score)
print('-------------------')
if player_score==computer_score:
print('平局')
elif player_score>computer_score:
print(player_name, '胜利')
else:
print(computer_name, '胜利')


特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线