python变量运算符分支结构循环结构及例题

第一周总结复习
python的[官方网站](http://python.org)
##变量
'''
1.变量的作用:变量是数据的载体

2.变量的命名:字母数字下划线开头,不能用数字开头
            不能用关键字,特殊符号 $等开头
大小写敏感(a 和 A 不一样)
PEP 8 要求:
用小写字母拼写,多个单词用下划线隔开
受保护的实例属性用单个下划线开头
私有的实例属性用两个下划线开头
3.变量的类型: int(),float(),str(), chr(), ord()--将字符串转化成对应的编码

'''
##运算符
'''
1.赋值运算符: =(赋值)、+=(加赋值)、-=(减赋值)、*=(乘赋值)、/=(除赋值)、%=(取余赋值)
2.算数运算符: +、-、*、/、%
3.关系运算符: ==(等于)、!=(不等于)、>(大于)、<(小于)、>=(大于等于)、<=(小于等于)
4.逻辑运算符:and(与)、or(或)、not(非)
5.身份运算符
'''
##分支结构
'''
'''
##循环结构
'''
1.while循环:只要某一条件成立,就不断执行循环体里的代码,直到该条件不再成立,条件是否成立同样是逻辑值来表示
2.for循环:循环的循环次数一般是事先定好的,将一个条件变量从某个岂是值开始,一直爹逮到某个终止值后结束
循环控制--pass:什么也不做,只是占据一行代码的位置;
continue:表示立即退出本轮循环,不运行本轮循环的后续代码,并继续执行接下来的循环;
break: 表示立即退出整个循环,后续循环不再执行
'''
python例题练习:(所有代码在python3.5环境下可运行)
1.输入三个数字,计算三角形面积周长:
import math
a = float(input("请输入三角形边第一条边长:"))
b = float(input("请输入三角形边第二条边长:"))
c = float(input("请输入三角形边第三条边长:"))
while True:
    if a + b > c and a + c > b and b + c > a:
          perimeter = a + b + c
          p = perimeter / 2
          g = math.sqrt(p * (p - a) * (p - b) * (p - c))
          print("三角形周长为:%.2f" % perimeter)
          print("三角形面积为: %.2f" % g)
    else:
        print("-------------This is not triangle!!----------------")
    break

 


2.用户输入数字,由电脑来猜:
from random import randint

user_input = int(input("please input the number you want computer guess>>>:"))
counter = 0
low = 1
high = 100
money = 1000
while True:
    guess = randint(low, high)
    print(guess)
    counter += 1
    if user_input == guess:
        print("congratulation, computer guess right")
        print("total guess:", counter)
        money += 200
        print("computer has: ",money)
        if money < 500:
           print(" computer you are fucking stupid!!!!")
        quit()
    elif user_input > guess:
        low = guess + 1
        print("computer guess a higher number")
        money -= 100
    else:  # user_input < guess
        high = guess - 1
        print("computer guess a lower number:")
        money -= 100

3.和电脑猜石头剪刀布:

from random import randint
while True:
    user_guess=int(input("1-- scisoor, 2--stone, 3--handkerchief:"))
    cm_guess = randint(1, 3)
    print(cm_guess)
    if user_guess == cm_guess:
        print("no one lose")
    elif user_guess == 1 and cm_guess == 2:
        print("user win")
    elif user_guess == 2 and cm_guess == 3:
        print("computer win")
    elif user_guess == 3 and cm_guess == 2:
        print("user win")
    else: #user_guess == 3 and cm_guess == 1:
        print("computer win")

4. 打印(2,100)所有素数

import math
def get_prime(maxNum):
    primeList = []
    for x in range(2, maxNum + 1):
        a = math.sqrt(x)
        for prime in primeList:
            if prime > a:
                primeList.append(x)
                break
            if x % prime == 0:
                break
        else:
            primeList.append(x)
    return primeList

print(get_prime(100))

5. 打印 '*'

row = int(input("please input the number of row:>>>>>"))
for i in range(row):
    for j in range(row):
        if j < row - i - 1:
            print(' ', end = '')
        else:
            print('*', end = '')
    print()

6. 打印三角形的大写字母,每行的字母一样:

for x in range(65, 70):
        for y in range(64, x):
            print(chr(x), end = '')
        print()

7. 找出1-10000的水仙花数:

from time import time
from math import sqrt
start = time()
for n in range(1, 10000):
my_sum = 0
for factor in range(1,int(sqrt(n)) + 1):
if n % factor == 0:
my_sum += factor
if factor != 1 and factor != n // factor:
my_sum += n // factor
if my_sum == n:
print(n)
end = time()
print((end - start), 's')

8. 五人抓鱼问题:A把鱼分成五份后扔走一条拿走自己那部分,BCDE用和A一样方法分鱼,求鱼一共有多少条(本题使用穷举法):

fish = 1
while True:
    total = fish
    is_enough = True
    for _ in range(5):
        if (total - 1) % 5 == 0:
            total = (total - 1) // 5 * 4
        else:
            is_enough = False
            break
    if is_enough:
        print("the min number of fish is:", fish)
        break

    fish += 1

 9. craps 赌博游戏

# craps 赌博游
from random import randint

money = 1000
while money > 0:
print('players money:', money)
while True:
debt = int(input('please debt:'))
if 0 < debt <= money:
break
face1 = randint(1, 6)
face2 = randint(1, 6)
go_on = False
first_point = face1 + face2
print('玩家摇出了%d点' %first_point)
if first_point == 7 or first_point == 11:
print(" you win!")
money += debt

print("user have: %d" % money)
elif first_point == 2 or first_point == 3 or first_point == 12:
print(" computer win!")
money -= debt

print("user have: %d" % money)
else:
go_on = True
while go_on:
face1 = randint(1, 6)
face2 = randint(1, 6)
current_point = face2 + face1
print('玩家摇出了%d点' % current_point)
if current_point == 7:
print("user lose!")
money -= debt

go_on = False
print("user have: %d" % money)
elif current_point == first_point:
print("user win!")
money += debt

go_on = False
print("user have: %d" %money)

转载于:https://www.cnblogs.com/charlieyucao/p/8493104.html

你可能感兴趣的:(python变量运算符分支结构循环结构及例题)