python幾個小程式
阿新 • • 發佈:2019-01-23
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") break else: 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: break else: temp = num if temp <=5: while temp: temp = temp - 1 print("*",end="") print("\n") else: temp = 9 - temp while temp: temp = temp - 1 print("*",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 = 1
print(" ",end="")
k = 1
print("\n")