you? How old are you? How old are you? Traceback (most recent call last): File "ext3.py", line 8, in <module> print("I'm " + age + ' years old.') TypeError: cannot concatenate 'str' and 'int' objects
My name is Tim. >>> print(intro[12]) i >>> intro[12] = 'o' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment
This is a tuple. site = ('http://tw.pycon.org',) # This is a list. people = ['Michael', 'Juan', 'Curtis'] # Trailing commas are fine. people = ['Michael', 'Juan', 'Curtis',]
at the end. names.append('Wilmer') # Insert an item at a certain index. names.insert(0, 'Ruben') # Remove first occurrence of an item. names.remove('Travis') # Remove by index. del names[3]
item. info['location'] = 'Taipei' # Update an existing item. info['name'] = 'Mosky' # Remove an item. del info['location'] # Check whether an item is in the dict. del names[3]
10 Enter 'y' to continue: y What to buy: Oranges Price: 8 Enter 'y' to continue: Item Price -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ Apples 10 Oranges 8
10 What to buy: Oranges Price: 8 What to buy: Item Price -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ Apples 10 Oranges 8
name = raw_input('What to buy: ') price = raw_input('Price: ') if not name or not price: break items.append({'name': name, 'price': price}) accountant4.py (1/2)
name = raw_input('What to buy: ') price = raw_input('Price: ') if not name or not price: break items.append({'name': name, 'price': price}) print_header() print_items(items)
i = raw_input('Select an index: ') try: number = numbers[int(i)] except IndexError: print('No such index!') else: print('numbers[{}] = {}'.format( i, number, ))
raw_input('Give me a name: ') if not name: raise ValueError return name try: name = ask_for_name() except ValueError: print('No valid name!') else: print('The name is {}'.format(name))