Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "metadata": {
- "name": "Learn Python The Hard Way exe 32"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "raw",
- "metadata": {},
- "source": "from http://learnpythonthehardway.org/book/ex32.html\nremember he wrote it for python 2.x . Mine are for python3.x"
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "the_count = [1,2,3,4,5]\nfruits = ['apples','oranges','pears','apricots']\nchange = [1, 'pennies', 2, 'dimes',3, 'quarters']\n\n#this first of for-loop goes through\n\nfor number in the_count:\n print (\"This is count {}\".format(number))\n \nfor fruit in fruits:\n print (\"A fruit of type: {}\".format(fruit))\n\n#here is a mixed list\n#not sure what is going on with the %r and i\nfor i in change:\n print (\"I got %r\" %i)\n \n\n\n\n\n\n\n\n",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "This is count 1\nThis is count 2\nThis is count 3\nThis is count 4\nThis is count 5\nA fruit of type: apples\nA fruit of type: oranges\nA fruit of type: pears\nA fruit of type: apricots\nI got 1\nI got 'pennies'\nI got 2\nI got 'dimes'\nI got 3\nI got 'quarters'\n"
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "raw",
- "metadata": {},
- "source": "Let's try that again... with .format"
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "\nfor i in change:\n print (\"I got {}\" %i.format(change))\n \n",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "ename": "AttributeError",
- "evalue": "'int' object has no attribute 'format'",
- "output_type": "pyerr",
- "traceback": [
- "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
- "\u001b[1;32m<ipython-input-4-ab5e2831c7b7>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mchange\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"I got {}\"\u001b[0m \u001b[1;33m%\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mchange\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
- "\u001b[1;31mAttributeError\u001b[0m: 'int' object has no attribute 'format'"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "raw",
- "metadata": {},
- "source": "That didn't work... so we'll try something different."
- },
- {
- "cell_type": "raw",
- "metadata": {},
- "source": ""
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "\nfor i in change:\n print (\"I got {}\".format(change))\n \n",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "I got [1, 'pennies', 2, 'dimes', 3, 'quarters']\nI got [1, 'pennies', 2, 'dimes', 3, 'quarters']\nI got [1, 'pennies', 2, 'dimes', 3, 'quarters']\nI got [1, 'pennies', 2, 'dimes', 3, 'quarters']\nI got [1, 'pennies', 2, 'dimes', 3, 'quarters']\nI got [1, 'pennies', 2, 'dimes', 3, 'quarters']\n"
- }
- ],
- "prompt_number": 6
- },
- {
- "cell_type": "raw",
- "metadata": {},
- "source": "That didn't work either...moving on"
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": "#we'll build a list\nelements =[]\n\n#then a range function\nfor i in range (0,6):\n print (\"Adding %d to the list.\" %i)\n #append is a function that lists understand\n elements.append(i)\n \n#now print them\nfor i in elements:\n print (\"element was: %d\" %i)\n\n\n\n",
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": "Adding 0 to the list.\nAdding 1 to the list.\nAdding 2 to the list.\nAdding 3 to the list.\nAdding 4 to the list.\nAdding 5 to the list.\nelement was: 0\nelement was: 1\nelement was: 2\nelement was: 3\nelement was: 4\nelement was: 5\n"
- }
- ],
- "prompt_number": 7
- }
- ],
- "metadata": {}
- }
- ]
- }
Add Comment
Please, Sign In to add comment