博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
course_2_assessment_6
阅读量:4056 次
发布时间:2019-05-25

本文共 2522 字,大约阅读时间需要 8 分钟。

1.Write a function, sublist, that takes in a list of numbers as the parameter. In the function, use a while loop to return a sublist of the input list. The sublist should contain the same values of the original list up until it reaches the number 5 (it should not contain the number 5).

def sublist(lst):    count = 0    lst1 = list()    while count < len(lst):        if lst[count] != 5:            lst1.append(lst[count])        else:            break        count = count + 1    return lst1fhand = input()fhand = fhand.split()print(sublist(fhand))

2.Write a function called check_nums that takes a list as its parameter, and contains a while loop that only stops once the element of the list is the number 7. What is returned is a list of all of the numbers up until it reaches 7.

def check_nums(lst):    count = 0    lst1 = list()    while count < len(lst):        if lst[count] != 7:            lst1.append(lst[count])        else:            break        count = count + 1    return lst1fhand = input()fhand = fhand.split()s = check_nums(fhand)print(s)

3.Write a function, sublist, that takes in a list of strings as the parameter. In the function, use a while loop to return a sublist of the input list. The sublist should contain the same values of the original list up until it reaches the string “STOP” (it should not contain the string “STOP”).

def sublist(lst):    count = 0    lst1 = list()    while count < len(lst):        if lst[count] != 'STOP':            lst1.append(lst[count])        else:            break        count = count + 1    return lst1fhand = input()fhand = fhand.split()print(sublist(fhand))

4.Write a function called stop_at_z that iterates through a list of strings. Using a while loop, append each string to a new list until the string that appears is “z”. The function should return the new list.

def stop_at_z(lst):    count = 0    lst1 = list()    while count < len(lst):        if lst[count] != 'z':            lst1.append(lst[count])        else:            break        count = count + 1    return lst1fhand = 'adsfwezdfdfe'print(stop_at_z(fhand))

5.Below is a for loop that works. Underneath the for loop, rewrite the problem so that it does the same thing, but using a while loop instead of a for loop. Assign the accumulated total in the while loop code to the variable sum2. Once complete, sum2 should equal sum1.

sum1 = 0lst = [65, 78, 21, 33]for x in lst:    sum1 = sum1 + xsum2 = 0count = 0while count < len(lst):    sum2 = sum2 + lst[count]    count = count + 1print(sum2)

转载地址:http://jirci.baihongyu.com/

你可能感兴趣的文章
Redis与Memcached的区别
查看>>
redis sharding方案
查看>>
程序员最核心的竞争力是什么?
查看>>
Node.js机制及原理理解初步
查看>>
linux CPU个数查看
查看>>
分布式应用开发相关的面试题收集
查看>>
简单理解Socket及TCP/IP、Http、Socket的区别
查看>>
利用HTTP Cache来优化网站
查看>>
利用负载均衡优化和加速HTTP应用
查看>>
消息队列设计精要
查看>>
分布式缓存负载均衡负载均衡的缓存处理:虚拟节点对一致性hash的改进
查看>>
分布式存储系统设计(1)—— 系统架构
查看>>
MySQL数据库的高可用方案总结
查看>>
常用排序算法总结(一) 比较算法总结
查看>>
SSH原理与运用
查看>>
SIGN UP BEC2
查看>>
S3C2440中对LED驱动电路的理解
查看>>
《天亮了》韩红
查看>>
Windows CE下USB摄像头驱动开发(以OV511为例,附带全部源代码以及讲解) [转]
查看>>
出现( linker command failed with exit code 1)错误总结
查看>>