网站建设资讯

NEWS

网站建设资讯

Python怎么实现剪刀石头布游戏

这篇文章主要介绍“Python怎么实现剪刀石头布游戏”,在日常操作中,相信很多人在Python怎么实现剪刀石头布游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python怎么实现剪刀石头布游戏”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

成都创新互联主营福绵网站建设的网络公司,主营网站建设方案,重庆APP开发,福绵h5微信平台小程序开发搭建,福绵网站营销推广欢迎福绵等地区企业咨询

srpgame.py

#!/urs/bin/env python
import random
all_choice = ['石头','剪刀','布']
win_list = [['石头','剪刀'],['剪刀','布'],['布','石头']]
prompt = """
(0) 石头
(1) 剪刀
(2) 布
Please input your choice(0/1/2): """
computer = random.choice(all_choice)
ind = int(input(prompt))
player = all_choice[ind]
print("Your choice: %s,Computer's choice: %s" %(player,computer))
if player == computer:
    print('\033[32;1m平局\033[0m')
elif [player,computer] in win_list:
    print('\033[31;1mYou WIN!!!\033[0m')
else:
    print('\033[31;1mYou LOSE!!!\033[0m')

演示

D:\Python scripts                         
λ  python3 .\srpgame.py                   
                                          
(0) 石头                                    
(1) 剪刀                                    
(2) 布                                     
Please input your choice(0/1/2): 0        
Your choice: 石头,Computer's choice: 剪刀     
You WIN!!!                                
D:\Python scripts                         
λ  python3 .\srpgame.py                   
                                          
(0) 石头                                    
(1) 剪刀                                    
(2) 布                                     
Please input your choice(0/1/2): 1        
Your choice: 剪刀,Computer's choice: 剪刀     
平局                                        
D:\Python scripts                         
λ  python3 .\srpgame.py                   
                                          
(0) 石头                                    
(1) 剪刀                                    
(2) 布                                     
Please input your choice(0/1/2): 2        
Your choice: 布,Computer's choice: 石头      
You WIN!!!                                
D:\Python scripts                         
λ  python3 .\srpgame.py                   
                                          
(0) 石头                                    
(1) 剪刀                                    
(2) 布                                     
Please input your choice(0/1/2): 0        
Your choice: 石头,Computer's choice: 布      
You LOSE!!!

到此,关于“Python怎么实现剪刀石头布游戏”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


分享题目:Python怎么实现剪刀石头布游戏
当前链接:http://njwzjz.com/article/gopgeh.html