鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 欧小麦

[见证历程] 笨办法学Python(python3.6)

[复制链接]
 楼主| 发表于 2018-8-15 16:22:11 | 显示全部楼层
本帖最后由 欧小麦 于 2018-8-15 16:33 编辑

练习30.else和if
people = input("people?")
cars = input("cars?")
trucks = input("trucks?")

if cars > people:
    print("we should take the cars.")
elif cars < people:
    print("we should not take the cars")
else:
    print("we can't decide.")
   
if trucks > cars:
    print("that's we could take the trucks.")
elif trucks < cars:
    print("maybe we could take the trucks.")
else:
    print("we still can't decide")

if people > trucks:
    print("alright, let's just take the trucks.")
else:
    print("fine, let's stay home then.")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-15 16:57:30 | 显示全部楼层
练习31.作出决定if-elif-else
print("you enter a dark room with two doors.do you go through door #1 or door #2?")

door = input("1 or 2?")

if door == "1":
    print("there's a giant bear here eating a cheese cake.what do you do?")
    print("1.thake the cake.\n2.scream at the bear.")

    bear = input("1 or 2?")

    if bear == "1":
        print("the bear eats your face off. good job")
    elif bear =="2":
        print("the bear eats your legs off. good job")
    else:
        print("well,bear runs away")

elif door == "2":
    print("you can see:\n1blueberries.\n2yellow jacket.\n3a key")

    answer = input("please...")

    if answer == "1" or answer == "2":
        print("you can go")
    else:
        print("drop the hole")

else:
    print("that's only a dream.good luck")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-15 17:42:51 | 显示全部楼层
练习32.循环和列表[,]
the_count = [1,2,3,4,5]
fruits = ['apple','oranges','pears','apricots']
change = [1,'pennies',2,'dimes',3,'quarters']

#第一种列表for循环
for number in the_count:
    print('this is count %d'%number)

#同上
for fruit in fruits:
    print("a fruit of type: %s"%fruit)

#通过%r循环输出混合列表
for i in change:
    print('I got %r'%i)

elements = []#空列表
for i in range(0,6):#0,1,2,3,4,5循环输出
    print("adding %d to the list"%i)
    elements.append(i)#添加到列表

for i in elements:#循环输出列表
    print("element was: %d"%i)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-15 17:54:18 | 显示全部楼层
练习33.while循环
def print_number(n):
    i = 0
    while i<n:
        print("at the top i is %d"%i)
        numbers.append(i)

        i += 1
        print("number now:",numbers)
        print("at the bottom i is %d"%i)

numbers = []

print_number(6)
print("the numbers:")

for num in numbers:
    print(num)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-16 14:55:37 | 显示全部楼层
练习38.列表操作
ten_things = "Apples Oranges Crows Telephon Light Sugar"

print("wait there are not 10 tings in that list .let's fix that")

stuff = ten_things.split(' ')#按空格分开
more_stuff = ['day','night','song','frisbee','corn','banana','girl','boy']

while len(stuff)!=10:#while循环,直到stuff的长度为10
    next_one = more_stuff.pop()#取出more_stuff的最后一个元素
    print("adding:",next_one)
    stuff.append(next_one)#添加到stuff里
    print('there are %d items noww.'%len(stuff))

print("there we go:",stuff)

print(stuff[1])#stuff的第二个元素
print(stuff[-1])  #stuff的最后一个元素
print(stuff.pop())#stuff的最后一个元素

print(' '.join(stuff))#将stuff里的元素用户空格连接
print('#'.join(stuff[3:5]))#第三、第四个元素用户#号连接


#列表是常用的数据结构之一,是数据的有序列表
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-16 15:36:32 | 显示全部楼层
练习39.字典,可爱的字典{:,}
states = {
    'Oregon':'OR',
    'Florida':'FL',
    'Califonia':'CA',
    'New York':'NY',
    'Michigan':'MI'}

cities = {
    'CA':'San Francisco',
    'MI':'Detroit',
    'FL':'Jacksonville'}
#添加数据
cities['NY'] = 'New York'
cities['OR'] = 'Portland'
#显示一些城市
print('-'*10)
print('NY State has:',cities['NY'])
print('OR State has:',cities['OR'])
#显示一些州
print('-'*10)
print("Michigan's abbreviation is:",states['Michigan'])
print("Florida's abbreviation is:",states['Florida'])
#用州 然后城市
print('-'*10)
print("Michigan has:",cities[states['Michigan']])
#显示每一个州、缩写
for abbrev,city in cities.items():
    print("%s has the city %s"%(abbrev,city))
#一次做两件事情
print('-'*10)
for state,abbrev in states.items():
    print("%s state is abbreviated %s and has city %s"%(state,abbrev,cities[abbrev]))


state = states.get('Texas')
if not state:
    print("Sorry,no Texas.")

city = cities.get('Tx','Does not exist')
print("the city fot the state 'TX' is:%s"%city)

#字典是另一常用数据结构。如果你要用一个非数字的key,使用 dict ,如果你需要有序的东西,使用 list。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-16 16:02:17 | 显示全部楼层
练习40.模块,类,对象
#类的简单工作原理:
类是用来创建迷你模块的蓝本或定义实例化是如何创建这些小模块,并在同一时间将其导入。
实例化仅仅是指通过类创建一个对象。
由此产生的迷你模块被称为对象,你可以将其分配给一个变量,让它开始运行

class Song(object):#定义类
    def __init__(self,lyrics):
        self.lyrics = lyrics

    def sing_me_a_song(self):
        for line in self.lyrics:
            print(line)

happy_bday = Song(["Happy birthday to you",
                   "I don't want to get sued",
                   "so I'll stop right there"])#实例化对象

bulls_on_parade = Song(["They rally around the family",
                        "With pockets full of shells"])#实例化对象

happy_bday.sing_me_a_song()#调用函数

bulls_on_parade.sing_me_a_song()#调用函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-12 21:55:58 | 显示全部楼层
欧小麦 发表于 2018-8-14 21:55
练习20.函数和文件
from sys import argv

2023年9月12日打卡
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-18 17:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表