第一章节测试

  大家在做第一章测试题时,需要复习如下相关知识点:编译型VS解释型、变量名规范、数据类型、程序交互、格式化输出、运算符、流程控制。
1.简述编译型与解释型语言的区别,且分别列出你知道的那些语言属于编译型,哪些属于解释型。
2.执行Python脚本的两种方式是什么?
3.布尔值分别有什么?
4.如何查看变量在内存中的地址?
5.实现用户输入用户名和密码,当用户名为seven且密码为123时,显示登录成功,否则登录失败!
1 user = 'seven'
2 pwd = 123
3 username = input('username:')
4 password = int(input('password:'))
5 if username == user and password == pwd:
6     print('Welcome to %s' % username)
7 else:
8     print('Wrong username or password')
View Code

6.实现用户输入用户名和密码,当用户名为seven且密码为123时,显示登录成功,否则登录失败,失败时允许重复输入三次。

 1 user = 'seven'
 2 pwd = 123
 3 count = 0  # 计数器
 4 while count < 3:
 5     username = input('username:')
 6     password = int(input('password:'))
 7     if username == user and password == pwd:
 8         print('Welcome to %s' % username)
 9         break
10     else:
11         print('Wrong username or password')
12     count += 1
View Code

7.实现用户输入用户名和密码,当用户名为seven或Alex且密码为123时,显示登录成功,否则登录失败,失败时允许重复输入三次。

 1 user = ['seven', 'alex']
 2 pwd = 123
 3 count = 0
 4 while count < 3:
 5     username = input('username:')
 6     password = int(input('password:'))
 7     if username in user:
 8         if password == pwd:
 9             print('Welcome to %s' % username)
10             break
11     else:
12         print('Wrong username or password')
13     count += 1
View Code

8.声明变量注意事项有哪些?

9.Python 单行注释和多行注释分别用什么?

10.编写成绩的小程序,成绩有ABCDE5个等级,与分数的对应关系如下:

A  90-100
B  80-89
C  60-79
D  40-59
E  0-39
要求:用户输入0-100的数字后,你能正确打印它的对应成绩
 1 grade = int(input('please your grade:'))
 2 if grade >= 100:
 3     print('grade is 0-100')
 4 elif 90 <= grade < 100:
 5     print('A')
 6 elif 80 <= grade < 89:
 7     print('B')
 8 elif 60 <= grade < 79:
 9     print('C')
10 elif 40 <= grade < 59:
11     print('D')
12 else:
13     print('E')
View Code

11.写代码

a.使用while循环实现输出2-3+4-5+6...+100的和

1 count = 2
2 s = 0
3 while count <= 100:  # loop 3 s = -1 count = 4 
4     if count % 2 == 0:
5         s += count
6     else:
7         s -= count  # 3
8     count += 1
9     print(s)
View Code

b.使用while循环实现输出1,2,3,4,5,8,9,11,12

1 count = 0
2 while count < 12:
3     count += 1
4     if count == 6:
5         continue
6     elif count == 10:
7         continue
8     print(count)
View Code

c.使用while循环输出100-50,从大到小,如100,99,98...,到50时,再从0循环输出到50,然后结束。

小白方法:

1 count = 100
2 while count >= 50:
3         print(count)
4         count -= 1
5 count = 0
6 while count <= 50:
7     print(count)
8     count += 1
View Cod
高级玩法:
1 count = 0
2 while count <= 100:
3     count += 1
4     if count <= 50:
5         print(100 - count)  # count = 1
6     else:
7         print(count - 51)  # count =51 0 52 1
View Code

d. 使用while 循环实现输出1-100内的所有奇数。

1 count = 0
2 while count < 100:
3     count += 1
4     if count % 2 != 0:
5         print(count)
View Code

e.使用while循环实现输出1-100内的所有偶数。

1 count = 0
2 while count < 100:
3     count += 1
4     if count % 2 == 0:
5         print(count)
View Code

12.制作趣味模板程序(编程题)

 需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好任意显示。

1 name = input("Name:").strip()
2 site = input("Site:").strip()
3 hobby = input("Hobby:").strip()
4 print("可爱的%s,最喜欢在%s看书。爱好是%s" % (name, site, hobby))
View Code

13.输入年份,判断该年份是否是闰年并输出结果。(编程题)

 需求:凡符合下面两个条件之一的年份就是闰年。(1)能被4整除但不能被100整除。(2)能被400整除。

1 while True:
2     year = int(input('Please is year:'))
3     if year % 4 == 0 and year % 100 != 0:
4         print("%s is leap year." % year)
5         break
6     elif year % 400 == 0:
7         exit("%s is a century" % year)
8     else:
9         exit("不是闰年")
View Code

 14.假设一年定期利率为3.25%,计算一下要过多少年,一万元的一年定期存款连本带息能翻翻?(编程题)

1 #  利息计算的基本公式为:利息=本金×存期时间×存款利率;
2 capital = 10000  # 本金
3 count = 0
4 while capital< 20000:
5     count += 1
6     interest = capital * 0.0325   # 利息
7     capital += interest  # 连本带息
8     print(capital, count)
View Code

15.使用while,完成以下图形的输出

*
**
***
****
*****
****
***
**
*

1 count = 0
2 while count < 10:
3     count += 1
4     if count <= 5:
5         print(count * '*')
6     else:
7         print((10-count) * '*')  # count 6
View Code

16.使用while循环实现1-100的整数相加

1 count = 0
2 s = 0
3 while count < 100:
4     count += 1
5     s += count
6     print(s)
View Code

17.一球从100米高度自由落下。每次落地后反跳回原高度的一半;在落下,求它在第10次落地时,共经过多少米?第10次反跳多高。

1 height = 100
2 count = 0
3 n = 100
4 while count < 10:
5     height = height / 2  # 反弹高度50,
6     n += height * 2  # 统计总长度
7     count += 1
8     print(count, height, n)
View Code

猜你喜欢

转载自www.cnblogs.com/gaojiangtao/p/9863873.html