本文主要是介绍python几个小程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import random
while True:player = input("输入:|剪刀(0)| 石头(1)| 布(2)| 退出(q)|:\n")if(player == "q" or player == "0" or player == "1" or player == "2"): if player == "q":print("欢迎再来\n")breakelse:player = int(player)computer = random.randint(0,2)if (computer > player):print("你输了!再来!\n")elif (computer == player):print("平局\n")else:print("你赢了!\n")else:print("输入错误!重新输入\n")continue
python实现:如下图形
*
**
***
****
*****
***
**
*
num = 1
while True:if num == 9: breakelse:temp = numif temp <=5:while temp:temp = temp - 1print("*",end="")print("\n")else:temp = 9 - tempwhile temp:temp = temp - 1print("*",end="")print("\n")num += 1
形状:
*
***
*****
*******
*********
python代码:
num = input("Input a number:\n")
num = int(num)
for n in range(0,num):for i in range(0,num-n-1):print(" ",end="")for j in range(0,2*n+1):print("*",end="")print("\n")
C++代码:
#include<iostream>
#include<stdio.h>
using namespace std;class Shape
{
public:Shape(int num):len(num){}~Shape(){}void painShape();
private:int len;
};void Shape::painShape()
{for(int i = 0;i < len; i++){for(int j = 0;j < len-i-1;j++){printf(" ");}for(int k = 0 ; k < 2*i+1;k++){printf("*");}printf("\n");}
}
int main()
{Shape shape(5);shape.painShape();return 0;
}
图形:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
python代码:
k = 1;
num = input("Input a number:\n")
num = int(num)
for n in range(0,num):for i in range(0,num-n-1):print(" ",end="")for j in range(0,2*n+1):if k == 1:k = 0;print("*",end="")else:k = 1print(" ",end="")k = 1print("\n")
这篇关于python几个小程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!