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:
                print(‘Enjoy.‘)
                films[choice][1] = films[choice][1] - 1
            else:
                print(‘Sorry, we are sold out.‘)
        else:
            print("Sorry, you are too young to watch the film.")

    else:
        print(‘Sorry, we do not have this film.‘)
时间: 2024-08-01 10:44:15

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

How to learn Python

https://www.udemy.com/python-programming-for-real-life-networking-use/ https://pynet.twb-tech.com/blog/python/books-beginners.html Python Book Recommendations for Beginners (2014-01-17) By Kirk Byers You have decided to learn Python, but which Python

[IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果你觉得英文版看着累,当当网有中文版,也有电子版可以选择. 我试着将其中的代码更新到Python 3.同时附上一些自己的初学体会,希望会对你有帮助. 中文版有人把书名翻译为<笨办法学python>,其实我觉得叫做<学Python,不走寻常路>更有意思些. 作者的意思你可以在序言中详细了解

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