Variables with more than one value


In [1]:
which_one = int(input("What month (1-12)? "))
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
          'August', 'September', 'October', 'November', 'December']
 
if 1 <= which_one <= 12:
    print("The month is", months[which_one - 1])


What month (1-12)? 4
The month is April

In [2]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=which_one+%3D+int(input(%22What+month+(1-12)%3F+%22))%0D%0Amonths+%3D+%5B'January',+'February',+'March',+'April',+'May',+'June',+'July',%0D%0A++++++++++'August',+'September',+'October',+'November',+'December'%5D%0D%0A+%0D%0Aif+1+%3C%3D+which_one+%3C%3D+12%3A%0D%0A++++print(%22The+month+is%22,+months%5Bwhich_one+-+1%5D)&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%224%22%5D&curInstr=5&codeDivWidth=350&codeDivHeight=400"> </iframe>


More features of lists


In [3]:
demolist = ["life", 42, "the universe", 6, "and", 9]
print("demolist = ",demolist)
demolist.append("everything")
print("after 'everything' was appended demolist is now:")
print(demolist)
print("len(demolist) =", len(demolist))
print("demolist.index(42) =", demolist.index(42))
print("demolist[1] =", demolist[1])
 
# Next we will loop through the list
for c in range(len(demolist)):
    print("demolist[", c, "] =", demolist[c])
 
del demolist[2]
print("After 'the universe' was removed demolist is now:")
print(demolist)
if "life" in demolist:
    print("'life' was found in demolist")
else:
    print("'life' was not found in demolist")
 
if "amoeba" in demolist:
    print("'amoeba' was found in demolist")
 
if "amoeba" not in demolist:
    print("'amoeba' was not found in demolist")
 
another_list = [42,7,0,123]
another_list.sort()
print("The sorted another_list is", another_list)


demolist =  ['life', 42, 'the universe', 6, 'and', 9]
after 'everything' was appended demolist is now:
['life', 42, 'the universe', 6, 'and', 9, 'everything']
len(demolist) = 7
demolist.index(42) = 1
demolist[1] = 42
demolist[ 0 ] = life
demolist[ 1 ] = 42
demolist[ 2 ] = the universe
demolist[ 3 ] = 6
demolist[ 4 ] = and
demolist[ 5 ] = 9
demolist[ 6 ] = everything
After 'the universe' was removed demolist is now:
['life', 42, 6, 'and', 9, 'everything']
'life' was found in demolist
'amoeba' was not found in demolist
The sorted another_list is [0, 7, 42, 123]

In [4]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=demolist+%3D+%5B%22life%22,+42,+%22the+universe%22,+6,+%22and%22,+9%5D%0D%0Aprint(%22demolist+%3D+%22,demolist)%0D%0Ademolist.append(%22everything%22)%0D%0Aprint(%22after+'everything'+was+appended+demolist+is+now%3A%22)%0D%0Aprint(demolist)%0D%0Aprint(%22len(demolist)+%3D%22,+len(demolist))%0D%0Aprint(%22demolist.index(42)+%3D%22,+demolist.index(42))%0D%0Aprint(%22demolist%5B1%5D+%3D%22,+demolist%5B1%5D)%0D%0A+%0D%0A%23+Next+we+will+loop+through+the+list%0D%0Afor+c+in+range(len(demolist))%3A%0D%0A++++print(%22demolist%5B%22,+c,+%22%5D+%3D%22,+demolist%5Bc%5D)%0D%0A+%0D%0Adel+demolist%5B2%5D%0D%0Aprint(%22After+'the+universe'+was+removed+demolist+is+now%3A%22)%0D%0Aprint(demolist)%0D%0Aif+%22life%22+in+demolist%3A%0D%0A++++print(%22'life'+was+found+in+demolist%22)%0D%0Aelse%3A%0D%0A++++print(%22'life'+was+not+found+in+demolist%22)%0D%0A+%0D%0Aif+%22amoeba%22+in+demolist%3A%0D%0A++++print(%22'amoeba'+was+found+in+demolist%22)%0D%0A+%0D%0Aif+%22amoeba%22+not+in+demolist%3A%0D%0A++++print(%22'amoeba'+was+not+found+in+demolist%22)%0D%0A+%0D%0Aanother_list+%3D+%5B42,7,0,123%5D%0D%0Aanother_list.sort()%0D%0Aprint(%22The+sorted+another_list+is%22,+another_list)&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%5D&curInstr=34&codeDivWidth=350&codeDivHeight=400"> </iframe>



In [ ]:


In [5]:
menu_item = 0
namelist = []
while menu_item != 9:
    print("--------------------")
    print("1. Print the list")
    print("2. Add a name to the list")
    print("3. Remove a name from the list")
    print("4. Change an item in the list")
    print("9. Quit")
    menu_item = int(input("Pick an item from the menu: "))
    if menu_item == 1:
        current = 0
        if len(namelist) > 0:
            while current < len(namelist):
                print(current, ".", namelist[current])
                current = current + 1
        else:
            print("List is empty")
    elif menu_item == 2:
        name = input("Type in a name to add: ")
        namelist.append(name)
    elif menu_item == 3:
        del_name = input("What name would you like to remove: ")
        if del_name in namelist:
            # namelist.remove(del_name) would work just as fine
            item_number = namelist.index(del_name)
            del namelist[item_number]
            # The code above only removes the first occurrence of
            # the name.  The code below from Gerald removes all.
            # while del_name in namelist:
            #       item_number = namelist.index(del_name)
            #       del namelist[item_number]
        else:
            print(del_name, "was not found")
    elif menu_item == 4:
        old_name = input("What name would you like to change: ")
        if old_name in namelist:
            item_number = namelist.index(old_name)
            new_name = input("What is the new name: ")
            namelist[item_number] = new_name
        else:
            print(old_name, "was not found")
 
print("Goodbye")


--------------------
1. Print the list
2. Add a name to the list
3. Remove a name from the list
4. Change an item in the list
9. Quit
Pick an item from the menu: 9
Goodbye

In [6]:
%%html
<iframe width="800" height="500" frameborder="0" src="http://pythontutor.com/iframe-embed.html#code=menu_item+%3D+0%0D%0Anamelist+%3D+%5B%5D%0D%0Awhile+menu_item+!%3D+9%3A%0D%0A++++print(%22--------------------%22)%0D%0A++++print(%221.+Print+the+list%22)%0D%0A++++print(%222.+Add+a+name+to+the+list%22)%0D%0A++++print(%223.+Remove+a+name+from+the+list%22)%0D%0A++++print(%224.+Change+an+item+in+the+list%22)%0D%0A++++print(%229.+Quit%22)%0D%0A++++menu_item+%3D+int(input(%22Pick+an+item+from+the+menu%3A+%22))%0D%0A++++if+menu_item+%3D%3D+1%3A%0D%0A++++++++current+%3D+0%0D%0A++++++++if+len(namelist)+%3E+0%3A%0D%0A++++++++++++while+current+%3C+len(namelist)%3A%0D%0A++++++++++++++++print(current,+%22.%22,+namelist%5Bcurrent%5D)%0D%0A++++++++++++++++current+%3D+current+%2B+1%0D%0A++++++++else%3A%0D%0A++++++++++++print(%22List+is+empty%22)%0D%0A++++elif+menu_item+%3D%3D+2%3A%0D%0A++++++++name+%3D+input(%22Type+in+a+name+to+add%3A+%22)%0D%0A++++++++namelist.append(name)%0D%0A++++elif+menu_item+%3D%3D+3%3A%0D%0A++++++++del_name+%3D+input(%22What+name+would+you+like+to+remove%3A+%22)%0D%0A++++++++if+del_name+in+namelist%3A%0D%0A++++++++++++%23+namelist.remove(del_name)+would+work+just+as+fine%0D%0A++++++++++++item_number+%3D+namelist.index(del_name)%0D%0A++++++++++++del+namelist%5Bitem_number%5D%0D%0A++++++++++++%23+The+code+above+only+removes+the+first+occurrence+of%0D%0A++++++++++++%23+the+name.++The+code+below+from+Gerald+removes+all.%0D%0A++++++++++++%23+while+del_name+in+namelist%3A%0D%0A++++++++++++%23+++++++item_number+%3D+namelist.index(del_name)%0D%0A++++++++++++%23+++++++del+namelist%5Bitem_number%5D%0D%0A++++++++else%3A%0D%0A++++++++++++print(del_name,+%22was+not+found%22)%0D%0A++++elif+menu_item+%3D%3D+4%3A%0D%0A++++++++old_name+%3D+input(%22What+name+would+you+like+to+change%3A+%22)%0D%0A++++++++if+old_name+in+namelist%3A%0D%0A++++++++++++item_number+%3D+namelist.index(old_name)%0D%0A++++++++++++new_name+%3D+input(%22What+is+the+new+name%3A+%22)%0D%0A++++++++++++namelist%5Bitem_number%5D+%3D+new_name%0D%0A++++++++else%3A%0D%0A++++++++++++print(old_name,+%22was+not+found%22)%0D%0A+%0D%0Aprint(%22Goodbye%22)&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&showOnlyOutputs=false&py=3&rawInputLstJSON=%5B%222%22,%22kwj%22,%229%22%5D&curInstr=28&codeDivWidth=350&codeDivHeight=400"> </iframe>



In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: