1.#输入a,b,c,d4个整数,计算a+b-c*d的结果
2401
第五种:
>>> for i in range(3):
... a*=7
...
>>> a
2401
>>> "*7"*4
'*7*7*7*7'
>>> "*7"*4[1:]
Traceback (most recent call last)
File "
TypeError: 'int' object has no at
>>> ("*7"*4)[1:]
'7*7*7*7'
>>> eval(("*7"*4)[1:])
2401
第七种:
>>> "*".join(a)
'7*7*7*7'
>>> eval("*".join(['7']*4))
2401
... return float("%.2f" %(5.0*100/9*(degree-32)/100))
...
>>> print FtoC(100)
37.78
#coding=utf-8
customer_price=float(raw_input("please input pay money:"))
if customer_price >=50 and customer_price<=100:
print "disconunt 10%% ,after discount you shoud pay %s" \
%(customer_price*(1-0.1))
elif customer_price >100:
print "disconunt 20%% ,after discount you shoud pay %s" \
%(customer_price*(1-0.2))
else:
print "disconunt 0%% ,after discount you shoud pay %s" \
%customer_price
>>> b=2
>>> a,b=b,a
>>> a,b
(2, 1)
#encoding=utf-8
a=input("please input a number as
time of year:")
if a%4==0 and a%400==0:
print "%d is a leap year!"%a
elif a%400==0:
print "%d is a leap year!"%a
else:
print "%d is not leap year!"%a
12.#一个足球队在寻找年龄在10岁到12岁的小女孩(包括10岁和12岁)加入。编写一个程序,询问用户的性别(m表示男性,f表示女性)和年龄,然后显示一条消息指出这个人是否可以加入球队,询问10次后,输出满足条件的总人数。
#encoding=utf-8
i=0
sum=0
while i<10:
a=raw_input("please tell me your gender:")
b=input("please tell me your age:")
if a=='f' and 10<=b<=12:
print "You can join in our basketball team!"
sum+=1
i+=1
print u"\n满足条件的人数为:%d人" %sum
生成随机整数,从1-5取出来
然后输入一个数字,来猜,如果大于,则打印bigger
小了,则打印less
如果相等,则打印equal
#coding=utf-8
import random
a=random.randint(1,5)
print "a is %d"%a
b=input("please input a number:")
if b>a:
print"bigger!"
elif b
elif b==a:
print "equal!"
16、打印10到1的数字:
17.写一个列表,倒序输出,用while循环实现
18.在15题的基础上,补充只允许输入3次,猜错了就退出
import random
target_number=random.randint(1,5)
print target_number
guess_times=3
while guess_times>0:
guess_number=int(raw_input("please input your guess number:"))
if guess_number>target_number:
print "bigger"
elif guess_number
print "less"
else:
print "you got it"
break
guess_times-=1
19.在18题的基础上,补充猜不中的情况下一直循环,直到猜中为止,计算猜的次数
import random
target_number=random.randint(1,5)
print target_number
guess_times=3
while guess_times>0:
guess_number=int(raw_input("please input your guess number:"))
if guess_number>target_number:
print "bigger"
elif guess_number
print "less"
else:
print "you got it"
break
guess_times-=1
input a number:13
>>> number=int(number)
>>> number
13
>>> for i in range(2,number):
... if number%i==0:
... print "not a prime"
... break
... else:
... print "%s is a prime" %number
...
13 is a prime
... result=1
... for i in range(1,n+1):
... result*=i
... return result
...
>>> print fact(4)
def fact(n):
#第一个条件:写出结束递归的分支
#第二个条件:写出自己调用自己的分支
if n<=1:
return 1
else:
return n*fact(n-1)
print fact(5)
# coding=utf-8
def f(n):
for i in n:
if isinstance(i,(tuple,dict,list)):
f(i),#,表示不打回车
else:
print i,#,表示不打回车
a = [1,2,(3,4,'sdsf',[5,6,'greg','kohi',
(7,8,9)]),'greg','pprgr']
f(a)
输出结果
1 2 3 4 sdsf 5 6 greg kohi 7 8 9 greg pprgr
22.#嵌套循环输出10-50中个位带有1-5的所有数字
方法一:
... for j in range(1,6):
... print str(i)+str(j)
.. if str(i)[1] in ["1","2","3","4","5"]:
.. print i
23、输入1-127的ascii码并输出对应字符
>>> for i in range(1,128):
... print chr(i)
输出结果:
24、计算一周有多少分钟、多少秒钟
>>> days_in_a_week=7
>>> hours_in_a_day=24
>>> mins_in_a_hour=60
>>> secs_in_a_minute=60
>>> print days_in_a_week*hours_in_a_day* mins_in_a_hour*secs_in_a_minute
604800
25、3个人在餐厅吃饭,想分摊饭费。总共花费35.27美元,他们还想给15%的消费。每个人该怎么付钱
pay 13.52 per person
for bread in ["1"]:
for hotdog in ["1","0"]:
for jam in ["1","0"]:
for jiemo in ["1","0"]:
for onion in ["1","0"]:
print bread+hotdog+jam+jiemo+onion
#coding=utf-8
cal_in_bread=1
cal_in_hotdog=1
cal_in_jam=1
cal_in_jiemo=1
cal_in_onion=1
for bread in ["1"]:
for hotdog in ["1","0"]:
for jam in ["1","0"]:
for jiemo in ["1","0"]:
for onion in ["1","0"]:
print "*"*50
print bread+hotdog+jam+jiemo+onion
print int(bread)*cal_in_bread+int(hotdog)*cal_in_hotdog+int(jam)*cal_in_jam+int(jiemo)*cal_in_jiemo+int(onion)*cal_in_onion
27、输入3个数,以逗号隔开,输出其中最大的数
>>> b=2
>>> c=3
>>> if a>b:
... max=a
... else:
... max=b
...
>>> if max>c:
... print max
... else:
... print c
# 获取最小值
if x > y:
smaller=y
biger=x
else:
smaller=x
biger=y
if biger%smaller==0:
hcf=smaller
else:
for i in range(1,(smaller + 1)/2):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
# 用户输入两个数字
num1 = int(input("输入第一个数字: "))
num2 = int(input("输入第二个数字: "))
print num1,"和", num2,"的最大公约数为", hcf(num1, num2)