Python编程 : 从入门到实践 课后习题五

5-3 外星人颜色#1 :假设在游戏中刚射杀了一个外星人,请创建一个名为alien_color 的变量,并将其设置为’green’ 、‘yellow’ 或’red’ 。 编写一条if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。 编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。

color = ["green","yellow","red"]
for alien_color in color: 
    if alien_color =="green":
        print("Congratulations ! You will win Five points !")
Congratulations ! You will win Five points !
alien_color = 'green'

#编写一条 if 语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了 5 个点。
if alien_color == 'green':
    print('you win 5 points!')

#编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本中未通过(未通过测试时没有输出)。
alien_color = 'red'
if alien_color == 'green':
    print('you win 5 points!')

you win 5 points!

5-4 外星人颜色#2 :像练习5-3那样设置外星人的颜色,并编写一个if-else 结构。 如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5个点。 如果外星人不是绿色的,就打印一条消息,指出玩家获得了10个点。 编写这个程序的两个版本,在一个版本中执行if 代码块,而在另一个版本中执行else 代码块。

alien_color = "green"
color = ["green","yellow","red"]
if alien_color in color:
        print("Congratulations ! You will win Five points !")
else:
        print("Congratulations ! You will win Ten points !")
Congratulations ! You will win Five points !
alien_color = "orange"
color = ["green","yellow","red"]
if alien_color in color:
        print("Congratulations ! You will win Five points !")
else:
        print("Congratulations ! You will win Ten points !")
Congratulations ! You will win Ten points !
# alien_color = 'green'

# if alien_color == 'green':
#     print('you win 5 points!')
# else:
#     print('you win 10 points!')
    
alien_color = 'red'

if alien_color == 'green':
    print('you win 5 points!')
else:
    print('you win 10 points!')

you win 10 points!

5-8 以特殊方式跟管理员打招呼 :创建一个至少包含5个用户名的列表,且其中一个用户名为’admin’ 。想象你要编写代码,在每位用户登录网站后都打印一条问 候消息。遍历用户名列表,并向每位用户打印一条问候消息。 如果用户名为’admin’ ,就打印一条特殊的问候消息,如“Hello admin, would you liketo seeastatus report?”。 否则,打印一条普通的问候消息,如“Hello Eric, thank you for logging in again”。

People = ["Toy","Jeck","Eric","Jean","admin","Rebot"]
for P in People:
    if P =="admin":
        print("Hello admin, would you like to seeastatus report?")
    else:
        print("Hello {}, thank you for logging in again !".format(P))
    
    
Hello Toy, thank you for logging in again !
Hello Jeck, thank you for logging in again !
Hello Eric, thank you for logging in again !
Hello Jean, thank you for logging in again !
Hello admin, would you like to seeastatus report?
Hello Rebot, thank you for logging in again !
name_list = ["Bob", "July", "Xiayu", "admin", "Cat"]
for i in name_list:
    if i == "admin":
        print("Hello %s, would you like to see a status report?" % i)
    else:
        print("Hello %s, thank you for logging in again" % i)

Hello Bob, thank you for logging in again
Hello July, thank you for logging in again
Hello Xiayu, thank you for logging in again
Hello admin, would you like to see a status report?
Hello Cat, thank you for logging in again

5-9 处理没有用户的情形 :在为完成练习5-8编写的程序中,添加一条if 语句,检查用户名列表是否为空。 如果为空,就打印消息“We need to find some users!”。 删除列表中的所有用户名,确定将打印正确的消息。

People = ["Toy","Jeck","Eric","Jean","admin","Rebot"]
while len(People)>0:
    print(People[-1] +"! You will not login !")
    People.pop()
if len(People)==0:
    print("We need to find some users!")
elif i == "admin":
     print("Hello %s, would you like to see a status report?" % i)
else:
    print("Hello %s, thank you for logging in again" % i)
      
Rebot! You will not login !
admin! You will not login !
Jean! You will not login !
Eric! You will not login !
Jeck! You will not login !
Toy! You will not login !
We need to find some users!
name_list = []
if len(name_list) == 0:
    print("We need to find some users!")
else:
    for i in name_list:
        if i == "admin":
            print("Hello %s, would you like to see a status report?" % i)
        else:
            print("Hello %s, thank you for logging in again" % i)

We need to find some users!

5-10 检查用户名 :按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。 创建一个至少包含5个用户名的列表,并将其命名为current_users 。 再创建一个包含5个用户名的列表,将其命名为new_users ,并确保其中有一两个用户名也包含在列表current_users 中。 遍历列表new_users ,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指 出这个用户名未被使用。 确保比较时不区分大消息;换句话说,如果用户名’John’ 已被使用,应拒绝用户名’JOHN’ 。

current_users = ["Bob","Linda","Tufei","Bob","Jean"]
new_users = ["Linda","Bob","Linda","Xiaxue","Tom"]
for i in current_users:
    for j in new_users:
        if i==j:
            print("{},已被使用,请更换用户名 !".format(i.lower()))
            break
        else:
            print("{},没被注册,可以使用 !".format(i.lower()))
            
bob,没被注册,可以使用 !
bob,已被使用,请更换用户名 !
linda,已被使用,请更换用户名 !
tufei,没被注册,可以使用 !
tufei,没被注册,可以使用 !
tufei,没被注册,可以使用 !
tufei,没被注册,可以使用 !
tufei,没被注册,可以使用 !
bob,没被注册,可以使用 !
bob,已被使用,请更换用户名 !
jean,没被注册,可以使用 !
jean,没被注册,可以使用 !
jean,没被注册,可以使用 !
jean,没被注册,可以使用 !
jean,没被注册,可以使用 !
current_users = ["Bob","Linda","Tufei","July","Jean"]
new_users = ["Linda","Bob","Tony","Xiaxue","Tom"]
for i in new_users:
    if i in current_users:
         print("{},已被使用,请更换用户名 !".format(i.lower()))
    else:
         print("{},没被注册,可以使用 !".format(i.lower()))
linda,已被使用,请更换用户名 !
bob,已被使用,请更换用户名 !
tony,没被注册,可以使用 !
xiaxue,没被注册,可以使用 !
tom,没被注册,可以使用 !
current_users = ["bob", "jane", "jack", "admin", "tom"]
new_users = ["Bob", "james", "NIck", "JACK", "name"]
for i in new_users:
    if i.lower() in current_users:
        print("%s 已经被使用,请更换用户名" % i)
    else:
        print("%s 没被注册,可以使用" % i)

Bob 已经被使用,请更换用户名
james 没被注册,可以使用
NIck 没被注册,可以使用
JACK 已经被使用,请更换用户名
name 没被注册,可以使用

5-11 序数 :序数表示位置,如1st和2nd。大多数序数都以th结尾,只有1、2和3例外。 在一个列表中存储数字1~9。 遍历这个列表。 在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内容应为1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每个序 数都独占一行。

number = [1,2,3,4,5,6,7,8,9]
for n in number:
    if n ==1:
        print("{}st".format(n),end=" ")
    elif n ==2:
        print("{}nd".format(n),end=" ")
    elif n == 3:
        print("{}rd".format(n),end=" ")
    else:
        print("{}th".format(n),end=" ")
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 
list=[]
for numbers in range(1,10):
    list.append(numbers)
print(list)
for number in list:
    if number==1:
        print(str(number)+'st')
    elif number==2:
        print(str(number)+'nd')
    elif number==3:
        print(str(number)+'rd')
    else:
        print(str(number)+'th')

[1, 2, 3, 4, 5, 6, 7, 8, 9]
1st
2nd
3rd
4th
5th
6th
7th
8th
9th

寻找两个列表中的相同元素
list1 = [‘张三’, ‘李四’, ‘王五’, ‘老二’]
list2 = [‘张三’, ‘李四’, ‘老二’, ‘王七’]

list1 = ['张三', '李四', '王五', '老二']

list2 = ['张三', '李四', '老二', '王七']

a = [x for x in list1 if x in list2] #两个列表表都存在
b = [y for y in (list1 + list2) if y not in a] #两个列表中的不同元素

print('a的值为:',a)
print('b的值为:',b)

c = [x for x in list1 if x not in list2]  #在list1列表中而不在list2列表中
d = [y for y in list2 if y not in list1]  #在list2列表中而不在list1列表中
print('c的值为:',c)
print('d的值为:',d)
a的值为: ['张三', '李四', '老二']
b的值为: ['王五', '王七']
c的值为: ['王五']
d的值为: ['王七']

利用Tkinter 创建窗口程序

import tkinter as tk  # 使用Tkinter前需要先导入
 
# 第1步,实例化object,建立窗口window
window = tk.Tk()
 
# 第2步,给窗口的可视化起名字
window.title('My Window')
 
# 第3步,设定窗口的大小(长 * 宽)
window.geometry('500x300')  # 这里的乘是小x
 
# 第4步,在图形界面上设定标签
l = tk.Label(window, text='你好!很高心遇见你 !', bg='red', font=('楷体', 12), width=30, height=2)
# 说明: bg为背景,font为字体,width为长,height为高,这里的长和高是字符的长和高,比如height=2,就是标签有2个字符这么高
 
# 第5步,放置标签
l.pack()    # Label内容content区域放置位置,自动调节尺寸
# 放置lable的方法有:1)l.pack(); 2)l.place();
 
# 第6步,主窗口循环显示
window.mainloop()
# 注意,loop因为是循环的意思,window.mainloop就会让window不断的刷新,如果没有mainloop,就是一个静态的window,传入进去的值就不会有循环,mainloop就相当于一个很大的while循环,有个while,每点击一次就会更新一次,所以我们必须要有循环
# 所有的窗口文件都必须有类似的mainloop函数,mainloop是窗口文件的关键的关键。
import tkinter as tk  # 使用Tkinter前需要先导入
 
# 第1步,实例化object,建立窗口window
window = tk.Tk()
 
# 第2步,给窗口的可视化起名字
window.title('My Window')
 
# 第3步,设定窗口的大小(长 * 宽)
window.geometry('500x300')  # 这里的乘是小x
 
# 第4步,在图形界面上设定标签
var = tk.StringVar()    # 将label标签的内容设置为字符类型,用var来接收hit_me函数的传出内容用以显示在标签上
l = tk.Label(window, textvariable=var, bg='blue', fg='white', font=('楷体', 12), width=30, height=2)
# 说明: bg为背景,fg为字体颜色,font为字体,width为长,height为高,这里的长和高是字符的长和高,比如height=2,就是标签有2个字符这么高
l.pack()
 
# 定义一个函数功能(内容自己自由编写),供点击Button按键时调用,调用命令参数command=函数名= False
def 敲击():
    globalif== False:= True
        var.set('您已点击了按钮!')
    else:= False
        var.set('')
 
# 第5步,在窗口界面设置放置Button按键
b = tk.Button(window, text='开始', font=('楷体', 12), width=10, height=1, command=敲击)
b.pack()
 
# 第6步,主窗口循环显示
window.mainloop()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章