Learn Python 010: Dictionary - Cinema Simulator

films = {
    ‘Finding Dory‘: [3, 5],
    ‘Bourne‘: [18, 7],
    ‘Tarzen‘: [15, 9],
    ‘Ghost Busters‘: [12, 5]

while True:
    choice = input(‘Please enter the selected film: ‘).title().strip()
    if choice in films:
        age = int(input(‘Please enter your age: ‘).strip())
        if age >= films[choice][0]:
            if films[choice][1] > 0:
                films[choice][1] = films[choice][1] - 1
                print(‘Sorry, we are sold out.‘)
            print("Sorry, you are too young to watch the film.")

        print(‘Sorry, we do not have this film.‘)
时间: 07-13

Learn Python 010: Dictionary - Cinema Simulator的相关文章

Learn Python 009: Dictionary

# create a dictionary students = {"Alice": 24, "Bob": 26, "Clark": 23, "Dan": 28, "Emma": 31} # add entry to a dictionary students['Fred'] = 27 # alter an entry students['Alice'] = 25 # delete entry del st

Python 字典(Dictionary) get()方法

描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 key -- 字典中要查找的键. default -- 如果指定键的值不存在时,返回该默认值值. 返回值 返回指定键的值,如果值不在字典中返回默认值None. 实例 以下实例展示了 get()函数的使用方法: #!/usr/bin/python dict = {'Name': 'Zara', 'A

Learn Python the Hard Way--Exercise 46

0. 缘起 <Learn Python the Hard Way>Exercise 46 要求安装四个python package pip, distribute, nose, virtualenv,(原书作者特别提醒: Do not just donwload these packages and install them by hand. Instead see how other people recommend you install these packages and use th

Learn Python From &#39;Head First Python&#39; [3](2) : Pickle

1.the use of 'with open... as ...' 2.the use of pickle(dump and load) for Step1: the 'with open ... as...' is the short format of 'try...except...finally' for Step2: you can store a list with pickle.dump() and get the content again with pickle.load()

Python 字典(Dictionary) setdefault()方法

描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: dict.setdefault(key, default=None) 参数 key -- 查找的键值. default -- 键不存在时,设置的默认键值;存在则不设置. 返回值 该方法没有任何返回值. 实例 以下实例展示了 setdefault()函数的使用方法: #!/usr/bin/pytho

Learn Python From &#39;Head First Python&#39; [2] : Sharing

1.publish 2.update print_lol PS: for Step 1. write the code in to a py file. put the py file into a folder named nester. new a py file named 'setup' and the content as below: setup( name = 'nester', version = '1.0.0', py_modules = ['nester'], author

Learn Python From &#39;Head First Python&#39; [1] : The List

1.the concept of List 2. how to define a method of myself 3.how to iterator a list PS: for Step 2,3. it is related with a key word: 'def'. we define a function like below. def function name: XXXXX e.g. list iterator : 1 def print_lol(the_list): 2 for

快速入门:十分钟学会PythonTutorial - Learn Python in 10 minutes

This tutorial is available as a short ebook. The e-book features extra content from follow-up posts on various Python best practices, all in a convenient, self-contained format. All future updates are free for people who purchase it. Preliminary fluf

learn python the hard way 习题45-制作一个游戏

小白自学python. from sys import exit from random import randint class Scene(object): def enter(self): print "it's no use" exit(1) class Engine(object): def __init__(self,scene_map): self.scene_map = scene_map def play(self): current_scene = self.sce

learn python the hard way—Python在Windows与Linux下的安装

1.Windows下安装https://www.python.org/downloads/下载相应版本,进行安装注意:将python的安装路径添加到系统环境变量的PATH路径下,这样才能在命令行窗口下输入python进入python环境 2.Linux下安装a.下载源码包 https://www.python.org/downloads/release/python-2710/b.解压 .tgz形式     tar -zxvf 压缩包 .tar.bz形式 tar -zjvf 压缩包c.进入解压后