您的位置:首页 > 编程语言 > Python开发

python简单练习

2017-07-18 19:27 225 查看
1.py  hello python

 print("Hello python")

print("hello world")

2.py  变量计算

 num1=100

num2=200

result=num1+num2

print(result)

3.py  名片制作

#输入名字

name = "fanfen"

#输入年龄

age = 28

#输入电话号码

mobile_num = 18875992320

#输入公司地址

add = "北京昌平"

#输出名片

print("==================\n姓名:%s\n年龄:%d\n手机号码:%d\n公司地址:%s\n=================="%(name,age,mobile_num,add))

4.py  if 语句判断是否成年

ge = int(input("请输入年龄:"))

if age>=18:

    print("已成年")

else:

    print("未成年")

print("程序结束")

5.py  if 嵌套上公交

#coding = utf-8

balance = int(input("Please input the Bus card balance:"))

emptySeat = int(input("Please input the empty seat number:"))

if balance>=2:

    print("You can get on the bus.")

    if emptySeat!=0:

        print("You can have a seat.")

    else: print("You can't have a seat.")

else:

    print("You can't get on the bus.")

6.py  elif语句猜拳游戏

import random

player = int(input('请输入:石头(0),剪刀(1),布(2):'))

computer = random.randint(0,2)

if (player == 0 and computer ==1)or(player == 1 and computer == 2) or (player == 2 and computer == 0):

    print("You win!")

elif (player == computer):

    print("呵呵,暂时平局")

else:

    print("You lose!")

7.py while累加1-100

#/usr/bin/python3

#coding=utf-8

i=0

sum = 0

while i<=100:

    if i%2 == 0:

        sum+=i

        print("i=%d sum=%d"%(i,sum))

    i+=1

print("程序结束")

8.py  while 打印*号

#coding = utf-8

n=1

while n<=5:

    i=1

    while i<=n:

        print("*",end="")

        i+=1

    print()
    n+=1

9.py while嵌套九九乘法表

#coding=utf-8

j=1

while j<=9:

    i=1

    while i<=j:

        print("%d*%d=%d "%(j,i,i*j),end="")

        i+=1

    print()

    j+=1

for i in range(1, 10):

    for j in range(1, i+1):

        print("%d * %d = %d " % (j, i, i*j), end="\t")
    print()

10.py while 倒三角形

#coding=utf-8

j=5

while j>=1:

    i=1

    while i<=j:

        print("*",end="")

        i+=1

    print()
    j-=1

11.py while 两三角形对接

#coding = utf-8

#N=int(input("Please input a number that above 7:"))

N=9

j=1

while abs(N-j)/2<=(N-1)/2:

    i=1

    while i<=abs(N-j)/2:

        print(" ",end="")

        i+=1

    i=1

    while i<=(N-abs(N-j)):

        print("*",end="")

        i+=1

    j+=2

    print()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: