course_2_assessment_6

本文介绍了五个使用while循环实现的Python函数,分别是sublist、check_nums、sublist(字符串版)和stop_at_z,它们分别用于从列表中截取直到特定数值或字符串出现前的子列表,并提供了一个将for循环转换为while循环的例子。

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 lst1
fhand = 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 lst1
fhand = 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 lst1
fhand = 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 lst1
fhand = '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 = 0
lst = [65, 78, 21, 33]

for x in lst:
    sum1 = sum1 + x

sum2 = 0
count = 0
while count < len(lst):
    sum2 = sum2 + lst[count]
    count = count + 1
print(sum2)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值