Python小白在校练习Project Four

1.编写一个程序打开文件romeo.txt并逐行读取。对于每一行,使用split函数将该行拆分为一个单词列表。对于每个单词,检查该单词是否已在列表中。如果单词不在列表中,请将其添加到列表中。程序完成后,按字母顺序对生成的单词进行sort和print。
<romeo.txt>
But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief

Enter file: romeo.txt
[‘Arise’, ‘But’, ‘It’, ‘Juliet’, ‘Who’, ‘already’, ‘and’, ‘breaks’, ‘east’, ‘envious’, ‘fair’, ‘grief’, ‘is’, ‘kill’, ‘light’, ‘moon’, ‘pale’, ‘sick’, ‘soft’, ‘sun’,‘the’, ‘through’, ‘what’, ‘window’, ‘with’, ‘yonder’]

file = input('Enter file:')
hand = open(file)
list = []
print('romeo.txt is split into four word lists:')
for line in hand:
    row = line.split()
    print(row)
    for word in row:
        if word not in list:
            list.append(word)
print('Sorted word list:')
list.sort()
print(list)

2.重写程序,提示用户输入一个数字列表,并在用户输入“done”时打印出数字的最大值和最小值。编写程序来存储用户在列表中输入的数字,并使用max()和min()函数计算循环完成后的最大和最小数。
Enter a number: 6
Enter a number: 2
Enter a number: 9
Enter a number: 3
Enter a number: 5
Enter a number: done
Maximum: 9.0
Minimum: 2.0

print('Please follow the instructions below.')
num_list = []
while True:
    number = input('Enter a number: ')
    if number == 'done':
        break
    else:
        try:
            num_list.append(float(number))
        except:
            print('Invalid input')
print('Maximum: ',max(num_list))
print('Minimum: ',min(num_list))
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值