其他分享
首页 > 其他分享> > 习题课3

习题课3

作者:互联网

习题1

  判断用户的键盘输入内容:

  - 如果都是数字,则将该数字扩大10倍,然后打印显示

  - 如果是字母,则在其后面增加"@python"后打印显示

  - 其他情况则将输入的内容按原样显示

user_input = input('input something:')

if user_input.isdigit():
    n = float(user_input)
    print(n * 10)
elif user_input.isalpha():
    print(user_input + "@python")
else:
    print(user_input)

习题2

  有字符串:Life is short You need python

  - 显示每个单词大写和小写两种状态

  - 统计每个单词的长度

s = "Life is short You need python"

for word in s.split():
    print(word, ': ', word.lower(), ", ", word.upper(), ', ', len(word))

习题3

  写一个猜数游戏程序

  - 计算机随机生成一个100以内的正整数

  - 用户通过键盘输入数字,猜测所生成的随机数

  - 对用户的输入次数不进行限制

import random
ran = random.randint(0,100)
print(ran)
switch = True
guess = 0

while switch:
    inp = int(input("请输入您要猜测的数字:"))
    guess += 1
    if inp == ran:
        print("恭喜你猜对了!")
        switch = False
print('您这次共猜了%s次'%guess)

习题4

  找出列表[2,4,-7,19,-2,-1.45]小于零的数

  某学生考试成绩:{'python':89, 'java':58, 'physics':65, 'math':49, 'Chinese':78},找出大于各科目平均分的学科

  计算1到99(含)的整数平方的和

>>> lst = [2, 4, -7, 19, -2, -1.45]
>>> [i for i in lst if i<0]
[-7, -2, -1.45]
>>> d = {'python':89, 'java':58, 'physics':65, 'math':49, 'Chinese':78}
>>> scores = d.values()
>>> mean = sum(scores) / len(scores)
>>> mean
67.8
>>> {k:v for k,v in d.items() if v>mean}
>>> {k:v for k,v in d.items() if v>mean}
{'python': 89, 'Chinese': 78}
>>> [i**2 for i in range(1, 100)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:word,python,习题课,print,input,习题,user
来源: https://www.cnblogs.com/zhaop8078/p/11817950.html