operation:
1. Description of bits, bytes relationship
2. Please write "genius" respectively utf-8 gbk coding and share-digit
4. Write the code, the following variables, as required to achieve each function:
name= " tianCAI"
SUMMARY a. Remove the variable name corresponding to both sides of the space values, and outputs after removal
b. determining whether the value corresponding to the variable name to "ti" at the beginning, and outputs the result
c. determining whether the value corresponding to the variable name to "I" at the end, and outputs the result
d. Place name corresponding to the value of the variable "n" is replaced with "p", and outputs the result
e. Insert in accordance with a value corresponding to the variable name "n" is divided, and outputs the result
f. I ask, a question on e get value after the split is what type?
g. the name corresponding to the variable value becomes capital, and outputs the result
h. Fold change values corresponding to the variable name in lower case, and the output
5. write code: While using the For loop and each element of each print string
6. Please be implemented in code:
li = ['alex','eric','rain']
Each use of the list underlined - a splice element to the string "alex_ eric rain"
7. To achieve a calculator integer addition:
Such as: content = input ( "Please enter Content:] # The user input: 9 or 5 + 5 + 5 + 9 or 9, and then divided again calculated
8. Calculate the content entered by the user has several integer?
Such as: content = input ( 'Enter content:) # as: asduiaf8781 23jkjsfd-213928
Requirements: waiting for the user to enter the name, location, hobbies, arbitrary reality according to the user's name and preferences
Such as: beloved dear xxx, like most places in XXX dry Xxx
Functional requirements:
It requires users to enter their total assets, such as: 2000
Show product list, allowing users to choose according to the number of goods, Add to Cart
Purchase, if the total is greater than the total commodity assets, suggesting insufficient account balance, otherwise, the purchase is successful.
goods= [
{"name": "电脑","price": 1999},
{"name":"鼠标","price": 10},
{"name":”游艇", "price": 20},
{"name":"美女","price": 998},
]
My answer:
1. Description of bits, bytes relationship
Bit: bit
Bytes: bytes
Byte = 8bit
2. Please write "genius" respectively utf-8 gbk coding and share-digit
UTF-8 letters are encoded as a byte, 3 bytes characters typically
GBK: 2Bytes on behalf of a Chinese character, 1Bytes represent an English character
Therefore, the "Genius" utf-8 encoded occupy 6 bytes, 48; 4 bytes encoded with gbk, 32
3. If there is a variable showing num = 14, int Please use the method, a minimum amount of change to obtain the number of bits can be used
num = 14
painful = num.bit_length ()
print (painful)
4. Write the code, the following variables, as required to achieve each function:
name= " tianCAI"
SUMMARY a. Remove the variable name corresponding to both sides of the space values, and outputs after removal
b. determining whether the value corresponding to the variable name to "ti" at the beginning, and outputs the result
c. determining whether the value corresponding to the variable name to "I" at the end, and outputs the result
d. Place name corresponding to the value of the variable "n" is replaced with "p", and outputs the result
e. Insert in accordance with a value corresponding to the variable name "n" is divided, and outputs the result
f. I ask, a question on e get value after the split is what type?
g. the name corresponding to the variable value becomes capital, and outputs the result
h.将name变量对应的值变小写,并输出结果
#a.
name = "tianCAI"
valus = name.strip()
print(name)
#b.
name = "tianCAI"
valus = name.startswith("ti")
print(valus)
#c.
name = "tianCAI"
valus = name.endswith("I")
print(valus)
#d.
name = "tianCAI"
valus = name.replace("n",'p')
print(valus)
#e.
name = "tianCAI"
valus = name.split('n')
print(valus)
#f.
<class 'list'>
name = "tianCAI"
valus = name.split('n')
print(type(valus))
#g.
name = "tianCAI"
valus = name.upper()
print(valus)
#h.
name = "tianCAI"
valus = name.lower()
print(valus)
5.写代码:使用While和For循环分别打印字符串每个元素
#while循环
name = "tiancai"
num = 0
while num < len(name):
print(name[num])
num += 1
#for循环
name = "tiancai"
for i in name:
print(i)
li = ['alex','eric','rain']
利用下划线将列表的每-一个元素拼接成字符串"alex_ eric rain"
li = ['alex','eric','rain']
value = '_'.join(li)
print(value)
如:content = input("请输入内容: ] # 如用户输入: 5+9 或5+9或5+9,然后进行分割再进行计算
matter = input('请输入内容:').strip()
list1 = matter.split('+')
num = 0
result = 0
while num < len(list1):
result = result+int(list1[num])
num += 1
print(result)
8.计算用户输入的内容中有几个整数?如:content = input('请输入内容: ) # 如: asduiaf8781 23jkjsfd-213928
content = input(">>>>:")
num = 0
for i in content:
if i.isdigit():
num += 1
print(num)
需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意现实
如:敬爱可亲的xxx,最喜欢在XXX地方干Xxx
name = input("Your name:")
place = input("Your favorite place:")
hobby = input("Your hobby:")
result = "敬爱可亲的%s,最喜欢在%s干%s" %(name,place,hobby)
print(result)
功能要求:
要求用户输入自己拥有总资产,例如: 2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
goods= [
{"name": "电脑","price": 1999},
{"name":"鼠标","price": 10},
{"name":”游艇", "price": 20},
{"name":"美女","price": 998},
]
goods= [
{"name": "电脑","price": 1999},
{"name":"鼠标","price": 10},
{"name":"游艇", "price": 20},
{"name":"美女","price": 998},
]
list1=[]
money = int(input("Your money:"))
for i,v in enumerate(goods,1):
print(i,v)
while True:
shop0 = input("Your number(按q退出):")
if shop0 == "q":break
else:shop = int(shop0)
if money > goods[shop-1]['price']:
money = money-goods[shop-1]['price']
print("添加成功,你的余额:",money)
list1.append(goods[shop-1]['name'])
else:print("You don't have enough money,请重新选择")
print("你购买的商品有:")
for m,n in enumerate(list1,1):
print(m,n)
print("你的余额为:",money)
teacher answer