python 第一章 初探Python课后练习题

2018/03/06 16:11
阅读数 24

1.输入1-127的ascii码并输出对应字符


#-*-coding:UTF-8-*-

for i in range(0,128):
print chr(i)


2.输入a,b,c,d4个整数,计算a+b-c*d的结果

#-*-coding:UTF-8-*-
a=int(raw_input ("please input number a: "))
b=int(raw_input ("please input number b: "))
c=int(raw_input ("please input number c: "))
d=int(raw_input ("please input number d: "))

result=a+b-c*d
print result


3.计算一周有多少分钟、多少秒钟

#-*-coding:UTF-8-*-

minutes=7*60*24
print("There are %d minutes in one week" % (minutes))

senconds=7*24*60*60

print("There are %d senconds in one week" % (senconds))


4.3个人在餐厅吃饭,想分摊饭费。总共花费35.27美元,他们还想给15%的小费。每个人该怎么付钱,编程实现

#-*-coding:UTF-8-*-
money=35.27*1.15

everone=money/3

print ("everyone should give %f money" % (everone))


5.计算一个12.5m X 16.7m的矩形房间的面积和周长

#-*-coding:UTF-8-*-
length=12.5
width=16.7
area=length*width

print ("the area is %f" % (area))

perimeter=2*length+2*width
print ("the perimeter is %f" % (perimeter))


6.怎么得到9 / 2的小数结果

#-*-coding:UTF-8-*-
a=9.00
b=2.00
result=a/b

print ("the result is %f" % (result))


7.python计算中7 * 7 *7 * 7,可以有多少种写法

#-*-coding:UTF-8-*-

result1=7*7*7*7

print result1

import math
result2=math.pow(7,4)

print result2

result3=1
for i in range(1,5):
result3=result3*7
print result3


8.写程序将温度从华氏温度转换为摄氏温度。转换公式为C = 5 / 9*(F - 32)

#-*-coding:UTF-8-*-

fahrenheit=int(raw_input("please input the Fahrenheit: "))

celsius=(fahrenheit-32)*5/9

print ("it is %f celsius" %(celsius))


9.一家商场在降价促销。如果购买金额50-100元(包含50元和100元)之间,会给10%的折扣,如果购买金额大于100元会给20%折扣。编写一程序,询问购买价格,再显示出折扣(10%或20%)和最终价格。

#一家商场在降价促销。如果购买金额50-100元(包含50元和100元)之间,会给10%的折扣,如果购买金额大于100元会给20%折扣。编写一程序,询问购买价格,再显示出折扣(10%或20%)和最终价格
#-*-coding:UTF-8-*-

price=int(raw_input("please input the price: "))
after_price=0
if price>=50 and price<=100:
after_price=price*(1-0.1)
elif price>100:
after_price=price*(1-0.2)
else:
after_price=price
print ("the price is %f." %(after_price))


10.判断一个数n能否同时被3和5整除

#-*-coding:UTF-8-*-

number=int(raw_input("please input a number: "))
if number%3==0 and number%5==0:
print ("this number %d can be divisible by 3 and 5." %(number))
else:
print ("this number %d can't be divisible by 3 and 5." %(number))

11.求1 + 2 + 3 +….+100

#-*-coding:UTF-8-*-
result=0
for i in range (1,101):
result=result+i;
i=i+1;
print result

 

12交换两个变量的值

#-*-coding:UTF-8-*-
x=int(raw_input("please input the number x:"))

y=int(raw_input("please input the number y:"))
tmp=x
x=y
y=tmp

print x,y


a=int(raw_input("please input the number a:"))

b=int(raw_input("please input the number b:"))
a,b = b,a

print a,b

13一个足球队在寻找年龄在10到12岁的小女孩(包括10岁和12岁)加入。编写一个程序,询问用户的性别(m表示男性,f表示女性)和年龄,然后显示一条消息指出这个人是否可以加入球队,询问10次后,输出满足条件的总人数。

#-*-coding:UTF-8-*-
result=0
for i in range(1,11):
sex = raw_input("please input your gender:input f for girl,input m for boy: ")
age = int(raw_input("please input your age: "))
if sex == "f" and 10<=age<=12:
print "welcome!you can join the team."
result+=1
else:
print "sorry you can't join the team"

print ("it is %d can join the team" %(result))

14 长途旅行中,刚到一个加油站,距下一个加油站还有200km,而且以后每个加油站之间距离都是200km。编写一个程序确定是不是需要在这里加油,还是可以等到接下来的第几个加油站再加油。 程序询问以下几个问题: 1)你车的油箱多大,单位升 2)目前油箱还剩多少油,按百分比算,比如一半就是0.5 3)你车每升油可以走多远(km) 提示: 油箱中包含5升的缓冲油,以防油表不准。

#-*-coding:UTF-8-*-
capacity=int(raw_input("please input your car's capacity: "))
percentage=float(raw_input("please input your gas liter remaining percentage now: "))
speed=int(raw_input("please input 1 L gas can run how many km: "))
n=int((capacity*percentage*speed-5)/200)
if capacity*percentage<5:
print "you need add gas here"
elif n==0:
print "you need add gas here"
else:
print ("you need add gas in %d gas station." %(n))

15 现有面包、热狗、番茄酱、芥末酱以及洋葱,数字显示有多少种订购组合,其中面包必订,0不订,1订,比如10000,表示只订购面包

#-*-coding:UTF-8-*-

num = 0
bread=1
if bool(bread):
for hotdog in range(0,2):
for ketchup in range(0,2):
for mustard in range(0,2):
for onion in range(0,2):
num = num + 1
print bread,hotdog,ketchup,mustard,onion
print num

16 基于上题:给出每种食物的卡路里(自定义),再计算出每种组合总共的卡路里

#15 现有面包、热狗、番茄酱、芥末酱以及洋葱,数字显示有多少种订购组合,其中面包必订,0不订,1订,比如10000, 表示只订购面包
#16 基于上题:给出每种食物的卡路里(自定义),再计算出每种组合总共的卡路里
#-*-coding:UTF-8-*-

total_calories = 0
bread_calories=int(raw_input("please input the bread calories: "))
hotdog_calories=int(raw_input("please input the hotdog calories: "))
ketchup_calories=int(raw_input("please input the ketchup calories: "))
mustard_calories=int(raw_input("please input the mustard calories: "))
onion_calories=int(raw_input("please input the onion calories: "))
cal1=0
cal2=0
cal3=0
cal4=0
cal5=0
bread=1
totalcalorie=0
if bool(bread):
cal1=bread_calories
for hotdog in range(0,2):
if hotdog==1:
cal2=hotdog_calories
else:
cal2=0
for ketchup in range(0,2):
if ketchup==1:
cal3=ketchup_calories
else:
cal3=0
for mustard in range(0,2):
if ketchup==1:
cal4=mustard_calories
else:
cal4=0
for onion in range(0,2):
if onion==1:
cal5=onion_calories
else:
cal5=0
totalcalorie=cal1+cal2+cal3+cal4+cal5

print bread,hotdog,ketchup,mustard,onion,totalcalorie

17输入5个名字,排序后输出

#-*-coding:UTF-8-*-

name_1 = raw_input("please input name_1:")
name_2 = raw_input("please input name_2:")
name_3 = raw_input("please input name_3:")
name_4 = raw_input("please input name_4:")
name_5 = raw_input("please input name_5:")
a = [name_1,name_2,name_3,name_4,name_5]
b = sorted(a)
print b

19输入一个正整数,输出其阶乘结果

#-*-coding:UTF-8-*-
n=int(raw_input("please input a number: "))
result=1
for n in range (1,n+1):
result=result*n

print result

20 计算存款利息 4种方法可选: 活期,年利率为r1; 一年期定息,年利率为r2; 存两次半年期定期,年利率为r3 两年期定息,年利率为r4 现有本金1000元,请分别计算出一年后按4种方法所得到的本息和。 提示:本息= 本金+ 本金* 年利率* 存款期

#-*-coding:UTF-8-*-
money=1000
r1=0.02
money1=1000*(1+r1)
r2=0.03
money2=1000*(1+r2)
r3=0.04
money3=1000*(1+r3)
r4=0.06
money4=1000*(1+r4)

print money1,money2,money3,money4

21输入3个数字,以逗号隔开,输出其中最大的数

num=raw_input ("please input three number split by , :")
num2=num.split(',')
print num2
max_number=max(num2)
print max_number

22输入一个年份,输出是否为闰年 是闰年的条件: 能被4整数但不能被100整除,或者能被400整除的年份都是闰年。

#-*-coding:UTF-8-*-

year=int(raw_input("please input a year: "))

if year%4==0 and year%100!=0 or year%400==0:
print "this is leap year!"
else:
print "this is not leap year!"

23求两个正整数m和n的最大公约数

#-*-coding:UTF-8-*-
m=int(raw_input("please input number m: "))
n=int(raw_input("please input number n: "))
result=[]
x=0
if m>n:
x=n
else:
x=m
for i in range (1,x):
if m%i==0 and n%i==0:
result.append(i)
print result
b=sorted(result)

print b[-1]

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部