前言: 编程中经典游戏练习-石头剪刀布,训练循环嵌套技巧。 程序的组织结构,顺序、循环、分支。 程序编写方法:IPO、流程图、伪代码。 # coding:utf-8 import random player_score = 0 comput
前言:
编程中经典游戏练习-石头剪刀布,训练循环嵌套技巧。
程序的组织结构,顺序、循环、分支。
程序编写方法:IPO、流程图、伪代码。
# coding:utf-8import randomplayer_score = 0computer_score = 0print('''* * * * * * * 欢迎来到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': breakprint('-------------------')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, '胜利')# coding:utf-8import randomrand = random.randint(1, 100)count = 1while count <= 10: number = eval(input("在我心中有个数,1-100之前,请你猜一猜:")) if number == rand: print("猜对了") break elif number > rand: print("大了") else: print("小了") count += 1if count <= 3: print("真聪明,一共猜了", count, "次")elif count <= 6: print("还可以,一共猜了", count, "次")else: print("猜的次数有点多啊,一共猜了", count, "次")