0. What would enter the following program?
def next():
print('我在next()函数里...')
pre()
def pre():
print('我在pre()函数里...')
next()
我在next()函数里...
我在pre()函数里...
Summary: In which joined the pre def next, but also pre defined below, it will all be printed.
4. The following visual program will print what?
var = ' Hi '
def fun1():
global var
var = ' Baby '
return fun2(var)
def fun2(var):
var += 'I love you'
fun3(var)
return var
def fun3(var):
var = ' 小甲鱼 '
print(fun1())
Var 1.global the first to define a global variable, so hi is changed, if at this time the last line plus print (var). It will print baby. If you remove the global, after a line is printed together with Hi
2.fun3 and not return a value, so that coding such as the following:
var = 'hi'
#注意这里一定要定义!因为在函数中的为局部变量
def fun3(var):
var = ' 小甲鱼 '
fun3(var)
It will print more empty, because there is no value to the fun3, and so on, so the source code fun3 invalid
value so the final will be printed: Baby i love you
0. write a function that determines whether the passed string argument "palindrome associated" (i.e., associated with a palindromic palindromic written in the form of couplets, can read along, for example, may be read backwards: Shanghai water from the sea).
自己的答案(错误!)
def back():
print('请输入查询:',end='')
temp = input()
i = 0
z = 0
while i < (len(temp)//2):
temp1 = temp[i]
if temp.find(temp1,i,i) == temp.rfind(temp1,i,i):
z = 2
i +=1
else:
z = 1
break
if z == 1 :
print('不是')
elif z == 2 :
print('是')
back()
find, rfind usage misunderstanding: the following example
str_super = "this is really a string example....wow!!!"
substr = "s"
print(str_super.rfind(substr))
print(str_super.rfind(substr, 0, 10))
print(str_super.rfind(substr, 10, 0))
print(str_super.find(substr))
print(str_super.find(substr, 0, 10))
print(str_super.find(substr, 10, 0))
————————————————
版权声明:本文为CSDN博主「追风筝的人000」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44901453/article/details/89598154
结果:
17
6
-1
3
3
-1
You can see, rfind role is to look backwards to the first s, then the output location, location and location for the same number of forward
if there is a large interest in God can help me rephrase the above procedure makes it feasible.
网上答案一:https://www.jianshu.com/p/f9bb090aaf24
def palindrome(string):
length = len(string)
last = length-1
length //= 2
flag = 1
for each in range(length)
#注意:range一个数是从0到这个数减一,不包括它本身
if string[each] != string[last]:
flag = 0
last -= 1
if flag == 1:
return 1
else:
return 0
string = input('请输入一句话:')
if palindrome(string) == 1:
print('是回文联!')
else:
print('不是回文联!')
The second was the use of a reverse list, it is necessary to advance the string becomes list form
def back():
list1 = []
list2 = []
print('请输入字符串:',end='')
temp = input()
list1 = list(temp)
list2 = reversed(list1)
if list1 == list(list2):
print('是回文联')
else:
print('八是哦')
back()
Here we must pay attention to the issue of reverse, and in writing this code, when I first used the list.reverse list (), and then assigned to list2.
The results showed that output list2 is empty, while list1 been reversed.
def back():
list1 = []
list2 = []
print('请输入字符串:',end='')
temp = input()
list1 = list(temp)
list2 = list1.reverse()
if list1 == list2:
print('是回文联')
else:
print('八是哦')
back()
结果:
请输入字符串:asddddd
['d', 'd', 'd', 'd', 'd', 's', 'a']
None
八是哦
This is because the reverse is list1 built-in functions, in order to save time, so directly list1 changes, due to the return value is null.
Therefore, if you want to assign to list here, but only to take a sequence operator reversed, and the list again in order to get the desired value.
1. Write a function, respectively, the statistics of the number of incoming string parameter (may be more than one parameter) of letters, spaces, numbers and other characters.
def number(*s):
length = len(s)
for z in range(length):
i = 0
a = 0
b = 0
c = 0
for each in s[z]:
if each.isdecimal():
i +=1
elif each.isalpha():
a +=1
elif each.isspace():
b +=1
else:
c +=1
print('第',z,'段字符含有数字:',i,'个 英文:',a,'个 空格:',b,'个 特殊字符:',c,'个')
number ('I love fish.com 123', 'I love you', 'you love 123')
note! ! ! ! ! 1. String built-in functions remember playing parentheses! ! ! !
2. If the input with the input 'I love fish.com 123', ' I love you', 'you love 123', the default input to a string, and not as above in which three strings numer ** (here to be understood!) **