首页
课程详情
师资团队
新闻资讯
开班报名
关于云脉
Java
Web
UI
数据分析
软件测试
Python
大数据
网络工程
练手迷你程序(附代码)

一、石头剪刀布游戏

  目标:创建一个命令行游戏,游戏者可以在石头、剪刀和布之间进行选择,与计算机PK。如果游戏者赢了,得分就会添加,直到结束游戏时,***终的分数会展示给游戏者。


  提示:接收游戏者的选择,并且与计算机的选择进行比较。计算机的选择是从选择列表中随机选取的。如果游戏者获胜,则增加1分。


import random  
choices = ["Rock", "Paper", "Scissors"]  
computer = random.choice(choices)  
player = False  
cpu_score = 0  
player_score = 0  
while True:  
    player = input("Rock, Paper or  Scissors?").capitalize()  
    # 判断游戏者和电脑的选择  
    if player == computer:  
        print("Tie!")  
    elif player == "Rock":  
        if computer == "Paper":  
            print("You lose!", computer, "covers", player)  
            cpu_score+=1  
        else:  
            print("You win!", player, "smashes", computer)  
            player_score+=1  
    elif player == "Paper":  
        if computer == "Scissors":  
            print("You lose!", computer, "cut", player)  
            cpu_score+=1 
        else:  
            print("You win!", player, "covers", computer)  
            player_score+=1  
    elif player == "Scissors":  
        if computer == "Rock":  
            print("You lose...", computer, "smashes", player)  
            cpu_score+=1  
        else:  
            print("You win!", player, "cut", computer)  
            player_score+=1  
    elif player=='E':  
        print("Final Scores:")  
        print(f"CPU:{cpu_score}")  
        print(f"Plaer:{player_score}")  
        break  
    else:  
        print("That's not a valid play. Check your spelling!")  
    computer = random.choice(choices)



二、随机密码生成器

目标:创建一个程序,可指定密码长度,生成一串随机密码。

提示:创建一个数字+大写字母+小写字母+特殊字符的字符串。根据设定的密码长度随机生成一串密码。

import random  
passlen = int(input("enter the length of password" ))  
s=" abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKL MNOPQRSTUVIXYZ!aN$x*6*( )?"  
p = ".join(random.sample(s,passlen ))  
print(p)  
----------------------------  
enter the length of password  
6  
Za1gB0


三、骰子模拟器

目的:创建一个程序来模拟掷骰子。

提示:当用户询问时,使用random模块生成一个1到6之间的数字。

import random;  
while int(input('Press 1 to roll the dice or 0 to exit:\n')): print( random. randint(1,6))  
--------------------------------------------------------------------  
Press 1 to roll the dice or 0 to exit  
1  
4


©2021 版权所有 技术支持:龙采科技
校区地址:哈尔滨市南岗区华润大厦10层
咨询热线:0451-51098402
备案号:黑ICP备2021003952号