记录一些代码

重量计算。月球上物体的体重是地球上的16.5%,地球上每年增长0.5kg,输出未来十年在地球和月球上的体重状况。

x = 55   
y = 55*0.165
earth = x + 0.5 * 10
moon =  y + 0.5 * 10
print('{:.2f}'.format(earth),'{:.2f}'.format(moon))

天天向上续,以七天为周期,前三天学习不增加,后四天每天增长百分之一

应该可以看做七天为周期,每隔七天增加百分之四

x = 1
for i in range(365):
    if i%7 == 0:
        x = x*(pow(1.01,4))
print(x)

天天向上续,每十天休息一天 周期重新计算

看做11天一个周期

x = 1
for i in range(365):
    if i%11 in [0]:
        x = x*(pow(1.01,4))
print(x)

回文数判断 输入一个数 他的倒叙和原数相同 则是回文数

m = input("")
n = m[::-1]
if n == m:
    print("yes")
else:
    print("no")

猜数游戏 程序中预设一个0-9的整数,让用户输入猜测的数字,若小于预设 输出“遗憾,太小了”,若大于预设则输出“遗憾,太大了”,若猜中则输出“预测N次 你猜中了”N是猜测次数

import random
x = 1
n = random.randint(0,9)

m = int(input(""))

while m != n:
   if m>n:
       print("遗憾,太大了")
       m = int(input(""))
       x+=1
   else:
       print("遗憾,太小了")
       m = int(input(""))
       x+=1
       
if m == n:
    print("预测{}次,你猜中了!".format(x))

统计一串用户输入的字符串中的空格 中英文字符 数字 的个数

s =input('请输入字符串:')
dic={'letter':0,'integer':0,'space':0,'other':0}
for i in s:
    if i >'a' and i<'z' or i>'A' and i<'Z' :
        dic['letter'] +=1
    elif i in '0123456789':
        dic['integer'] +=1
    elif i ==' ':
        dic['space'] +=1
    else:
        dic['other'] +=1
         
print('统计字符串:',s)
print(dic)
print('结果') 
for i in dic:
    print('%s='%i,dic[i])

最大公约数和最小公倍数

m = int(input(""))
n = int(input(""))
if m>n:
    o=n
else:
    o=m

for i in range(1,o+1):
    if (m%i == 0) and (n%i == 0):
        p = i
print("最大公约数是{}".format(p))
m = int(input(""))
n = int(input(""))
if m>n:
    o=n
else:
    o=m
for i in range(1,o+1):
    if (m%i == 0) and (n%i == 0):
        p = i
    
q = (m*n)/p       
 
print("最小公约倍数是{}".format(q))```
# 猜数游戏 预设范围变为0-100

猜数游戏 范围变为0-100

import random
x = 1
n = random.randint(0,100)

m = int(input(""))

while m != n:
    if m>n:
        print("遗憾,太大了")
        m = int(input(""))
        x+=1
    else:
        print("遗憾,太小了")
        m = int(input(""))
        x+=1
        
if m == n:
     print("预测{}次,你猜中了!".format(x))

猜数游戏,若输入不是规定格式 输出“输入内容必须为整数!”

用try函数

import random
try:
    
    x = 1
    n = random.randint(0,9)

    m = int(input(""))

    while m != n:
        if m>n:
            print("遗憾,太大了")
            m = int(input(""))
            x+=1
        else:
            print("遗憾,太小了")
            m = int(input(""))
            x+=1
        
        if m == n:
            print("预测{}次,你猜中了!".format(x))
except:
    print("输入内容必须是整数!")
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值