7-1要检查一个数是否大于 30 但小于或 等于 40,要用哪种 if 语句
my_number = int(raw_input("Enter your number:"))
print my_number,type(my_number)
if (my_number > 30 and my_number <40) or my_number == 40:
print '30 or <=40 '
else:
print '41 or over'```
####7-4
aword = raw_input("enter a q or Q: ")
if aword == "Q" or aword =='q'
print"you got a 'q'."
**同学的答案7-1**
if (30 < my_number <= 40):
print (…)
else:
print (…)
if (my_number<=30 or my_number>40):
print'no'
else:
print'yes'```
动手试一试
7-1
你在超市购物,如果购买金额小于10,有10%的优惠,如果金额大于10,有20%的优惠,设计一个程序,询问你的购买金额,并做出判断优惠,然后显示折扣和购物总金额。
import easygui
buy_price =easygui.integerbox("How much you pay for shoppping?")
if buy_price <=10:
easygui.msgbox("you got 10% salez")
buy_price =float(buy_price*(1-0.1))
easygui.msgbox("you cost " + str(buy_price))
elif buy_price >10:
easygui.msgbox( "you got 20% salez")
buy_price =float(buy_price*(1-0.2))
easygui.msgbox("you cost " + str(buy_price))```
修改后
import easygui
buy_price =easygui.integerbox("How much you pay for Items ?")
price = float(buy_price)
if price <=10.0:
discount = price0.10
elif price >10.0:
discount = price0.20
final_price = price - discount
easygui.msgbox("you got "+str(discount)+'off,your final_price was'+str(final_price))
```easygui.msgbox()
输出str,要用+连接,保证输出内容格式为str,需要转换int,float 格式。
而print 输出,可以包含不同格式,用“,”连接。```
7-2
**编程,正组建一个女子足球队,询问对方的年龄和性别,(m, f 表示男性和女性),并且年纪在10至12岁之间,才能加入足球队,提示是,先询问性别,性别不符合,可以不询问年纪。然后输出相应的提示**
football = raw_input("Are you m or f? ")
if football =="m":
print "Sorry,you can't join us."
elif football =="f":
age = int(raw_input("how old are you? "))
if age >=10 or age<=12:
print "You are allowed to joined our footballteam!"
else:
print "Sorry,you can't join us."```
7-3
你在长途旅行,刚到一个加油站,距下一个加油站还有 200 km。编写一个程序确定是不是需要在这里加油,还是可以等到下一个加油站再加油。
这个程序应当问下面几个问题。
你的油箱有多大(单位是升)?
油箱有多满(按百分比,例如,半满就是 50%)?
你的汽车每升油可以走多远(km)?
输出应该像这样:
Size of tank: 60
percent full: 40
km per liter: 10
You can go another 240 km
The next gas station is 200 km away
You can wait for the next station.```
或
Size of tank: 60
percent full: 30
km per liter: 8
You can go another 144 km
The next gas station is 200 km away
Get gas now!
代码清单7-3
how_size = int(raw_input("how size of your oil-box? liter:"))
percent = int(raw_input("how percent full of a oil-box? %:"))
p = percent/100.0
how_long= int(raw_input("how long km per a liter oil ? km:"))
print"Size of tank:",how_size
print "percent full:",percent
print "km per liter:",how_long
along=int(how_sizephow_long)
if along > 205:
print "The next gas station is 200km away" ,
print"You can wait for the next station."
elif along < 205:
print "You can go another",str(along)+"km",
print"The next gas station is 200 km away.",
print"Get gas now!"```
动手7-4
建立一个程序,用户必须输入密码才能使用这个程序。你当然知道密码(因为它会写在你的代码中)。不过,你的朋友要得到这个密码就必须问你或者直接猜,也可以学习足够的 Python 知识查看代码来找出密码!
*代码是写出来了,但是问题无法重复调用,除非是把这个逻辑放到一个定义函数里,再调用函数才行,才能达到输入密码三次就会锁定账户的功能,不然只会执行一次。写这段代码,是因为之前看过别的python书,里面有个例子就是写账户登录输入密码,所以就把那个借鉴过来了
- -||*
#-*- coding utf-8 -*-
import easygui
password_list =['*#*#','123456']
##password=easygui.enterbox('Enter the password')
tries=3
if tries>0:
password=easygui.enterbox('Enter the password')
if password == password_list[-1]:
easygui.msgbox( "You're in!")
elif password == password_list[0]:
new_password = easygui.enterbox("Enter a new password")
password_list.append(new_password)
easygui.msgbox("The password has changed sucessfully!")
else:
easygui.msgbox("Wrong password or invalid input")
tries = tries-1
easygui.msgbox(str(tries)+"time left")
else:
easygui.msgbox("Your account has suspended!")