第一题
i=input()
if i%2==1:
print('奇数')
else:
print('偶数')
第二题
count=0
for i in range(len(lb)):
temp=lb[i]
for j in range(len(temp)):
if temp[j]=='a':
count+=1
print(count)
第三题
def(a,b):
return a+b
第一题:
def odd_even(numbers):
while True:
prompt = "请输入一个数(输入”q“退出程序): "
numbers = input(prompt)
try:
if int(numbers) % 2 != 0:
print(str(numbers) + "是奇数.")
elif int(numbers) % 2 == 0:
print(str(numbers) + "不是奇数.")
except ValueError:
break
odd_even('')
输出结果:
请输入一个数(输入”315“退出程序): 0
0不是奇数.
请输入一个数(输入”315“退出程序): 9
9是奇数.
请输入一个数(输入”315“退出程序): q
Process finished with exit code 0
第二题:
def ct(words):
for word in words:
c = word.count('a')
print("字符串[" + word + "]中a的个数是:" + str(c))
d = ('adfwee', 'asdfee', 'asdfadwe', 'asdfafwewe')
ct(d)
输出结果
字符串[adfwee]中a的个数是:1
字符串[asdfee]中a的个数是:1
字符串[asdfadwe]中a的个数是:2
字符串[asdfafwewe]中a的个数是:2
第三题:
def a(b, c):
s = int(b) + int(c)
print("给定两数之和为:" + str(s))
a(4, 5)
输出结果
给定两数之和为:9