购物车程序的详解
shop = [
[ 'iphone8' , 5000 ] ,
[ 'kndle' , 988 ] ,
[ 'ps4 pro' , 2800 ] ,
[ 'psv' , 1200 ] ,
[ 'mp3' , 100 ]
]
shop_car = [ ]
saving = input ( "请输入预算:" )
if saving. isdigit( ) :
saving= int ( saving)
while True :
for i, v in enumerate ( shop, 1 ) :
print ( i, "-" , v)
choice = input ( "请选择商品编码到购物车:[确认:q]" )
if choice. isdigit( ) :
choice= int ( choice)
if choice > 0 and choice<= len ( shop) :
p_item = shop[ choice- 1 ]
if p_item[ 1 ] < saving:
saving -= p_item[ 1 ]
shop_car. append( p_item)
print ( "您选购的商品%s,剩余%s" % ( p_item, saving) )
else :
print ( "您的余额不足,剩余%s" % saving)
elif choice== "q" :
print ( "----您的购物车清单----" )
for i in shop_car:
print ( i)
guess = input ( "是否确认购买:q/n" )
if guess == "q" :
print ( "您已经购买%s,余额%s" % ( shop_car, saving) )
break
else :
print ( "欢迎再来" )
break
else :
print ( "输入编码无效" )
else :
print ( '输入金钱无效' )
字典
字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据。python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以无序存储的且key必须是可哈希的。可哈希表示不可变类型如:数字、字符串、元祖
不可变类型:整型,字符串,元祖
可变类型:列表,字典
dict1 = { "name" : "alex" , "age" : 29 , "hobby" : "game" , "is_handsome" : True }
print ( dict1[ "name" ]
dict1 = { [ 1 , 2 ] : "alex" , "age" : 29 , "hobby" : "game" , "is_handsome" : True }
dict1 = { "name" : [ "alex" , 29 ] , "age" : 29 , "hobby" : "game" , "is_handsome" : True }
dict2 = dict ( [ [ "name" , "alex" ] ] )
字典的操作
dict1 = { 'name' : "alex" }
dict1[ "age" ] = 18
print ( dict1)
rst = dict1. setdefault( "age" , "book" )
print ( rst)
rst1 = dict1. setdefault( "hobby" , "book" )
print ( rst1)
dict1 = { "name" : "alex" , "age" : 29 , "hobby" : "game" , "is_handsome" : True }
print ( dict1[ "name" ] )
print ( list ( dict1. keys( ) ) )
print ( list ( dict1. values( ) ) )
print ( list ( dict1. items( ) ) )
dict1 = { "name" : "alex" , "age" : 29 , "hobby" : "game" , "is_handsome" : True }
dict1[ "name" ] = 29
dict2 = { "name" : "alex" , "age" : 29 , "hobby" : "game" , "is_handsome" : True }
dict3 = { "1" : "111" , "2" : "222" , "3" : "333" }
dict2. update( dict3)
dict2 = { "name" : "alex" , "age" : 29 , "hobby" : "game" , "is_handsome" : True }
dict2. clear( )
print ( dic2)
del dict2[ "name" ]
print ( dict2. pop( "age" ) )
a = dict2. popitem( )
print ( a, dict2)
dic6 = dict . fromkeys( [ "host1" , "host2" , "host3" , "host4" ] , "test" )
dic5 = { "name" : "alex" , "age" : 18 , "hobby" : "python" }
for i in dic5:
print ( i, dic5[ i] )
for i, v for dic5. items( ) :
print ( i, v)
a = ( "hello" * 2 )
print ( a)
a = ( "helloworld" [ 2 : ] )
print ( a)
print ( 123 in "123456hello" )
print ( "alex is a good teacher" )
print ( "%s is a good teacher" % "alex" )
a = "123"
b = "abc"
c = "444"
d = "-----" . join( [ a, b, c] )
print ( d)
st = "hello kitty{name} is {age}"
print ( st. count( "l" ) )
print ( st. capitalize( ) )
print ( st. center( 50 , "-" ) )
print (st. casefold( ) )
print ( st. encode( ) )
print ( st. endswitch( "tty3" ) )
print ( st. startswitch( "he" ) )
print ( st. expandtabs( tabsize= 10 ) )
print ( st. find( "t" ) )
print ( st. format ( name = "Alex" , age = 37 ) )
print ( st. format_map( { "name" : "alex" , "age" : 22 } )
print ( st. index( "t" ) )
print ( "abc456" . isalnum( ) )
print ( "1231234" . isdeciaml( ) )
print ( "126323178" . isdigit( ) )
print ( "abc" . isidentifier( ) )
print ( "abc" . islower( ) )
print ( "ABC" . isupper( ) )
print ( " " . isspace( ) )
print ( "My Tittle" . istittle( ) )
print ( "My Tittle" . lower( ) )
print ( "My Tittle" . upper( ) )
print ( "My Tittle" . swapcase( ) )
print ( "My Tittle" . ljust( 50 , "*" ) )
print ( "My Tittle" . rjust( 50 , "*" ) )
print ( "\tMy Tittle\n" . lstrip( ) )
print ( "\tMy Tittle\n" . rstrip( ) )
print ( "\tMy Tittle\n" . strip( ) )
print ( "My Tittle\n" . replace( "Tittle" , "lesson" ) )
print ( "My Tittle" . split( " " ) )
print ( "My Tittle" . rsplit( " " ) )
print ( "My Tittle tittle" . title( ) )
print ( st. count( "l" ) )
print ( st. startswitch( "he" ) )
print ( st. center( 50 , "-" ) )
print ( st. find( "t" ) )
print ( st. format ( name = "Alex" , age = 37 ) )
print ( "My Tittle" . lower( ) )
print ( "My Tittle" . upper( ) )
print ( "\tMy Tittle\n" . strip( ) )
print ( "My Tittle\n" . replace( "Tittle" , "lesson" ) )
print ( "My Tittle" . split( " " ) )
作业:
打印省、市、县三级菜单
可返回上一级
可随时退出程序