学习06

写代码,有如下变量name=' aleX',please inplement each function as required

name =' aleX'
print(name.strip())## remove the space on both side, and print the result
print(name.startswith('al'))## judge wither name start with al, and print the result
print(name.endswith('X'))## judge wither name end with X, and print
print(name.replace('l','p'))## replace 'l' with 'p' in name , print
print(name.strip('l'))## split name according to l
print(name.upper())## upper the the value 
print(name.lower())## lower the value
print(name[1])## print the second str 
print(name[0:4])## print  frist three characters 
print(name[-1:-3:-1])## print the last two characters
print(name.index('e'))## output the index of e in name
print(name[0:4])
aleX
False
True
 apeX
 aleX
 ALEX
 alex
a
 ale
Xe
3
 ale

编写猜年龄游戏,有以下要求:

1. 可能会有用户会在输入年龄之后不小心输入空格,如18   ;,请做处理
2. 可能会有用户会恶意输入导致程序报错,如`逗你玩呀`,请做处理
3. 如果用户3次没有猜对,可以选择继续玩或退出(自定义退出条件)
4. 如果用户猜对了,可以在以下奖品中选择两件奖品(一次只能选择一件奖品):`{0:'buwawa',1:'bianxingjingang',2:'aoteman',3:'《python从入门到放弃》'}`
5. 用户选择奖品后退出程序,用户也可以不选择奖品直接退出程序。
prize={0:'buwawa',1:'bianxingjingang',2:'aoteman',3:'《python从入门到放弃》'}
real_age=18
i=0
while i<=3:
        age=input('please input age >>>')
        age=age.strip()
        if not age.isdigit():
            print('请输入数字')
        else:
             age=int(age)
             if age==real_age:
                 print('please choose typho: 0:buwawa,1:bianxingjingang,2:aoteman,3:《python从入门到放弃》 or just give up prize')
                 num=input('please choose number or give up')
                 if not num.isdigit():
                     print('给你机会也不要,结束了')
                     break
                 else:
                     num=int(num)
                     if num<4:
                         print(f'you get the {prize[num]}')
                         break
                     else:
                         print('真无语了,0~3都不会输入')
                         break
             else:
                 i+=1
                 if i<3:
                     if age<18:
                         print('smaller')
                     else:
                         print('bigger')
                 else:
                     result=input('continue or out>>>Y/y or N/n')
                     if result=='Y' or result=='y':
                         i=0
                     elif result=='N'or result=='n':
                         break
                     else:
                         print('sb')
                         break
please input age >>> ed


请输入数字


please input age >>> 阿松大


请输入数字


please input age >>> 威威


请输入数字


please input age >>> we


请输入数字


please input age >>> 12


smaller


please input age >>> 23


bigger


please input age >>> 32
continue or out>>>Y/y or N/n y
please input age >>> 23


bigger


please input age >>> 18


please choose typho: 0:buwawa,1:bianxingjingang,2:aoteman,3:《python从入门到放弃》 or just give up prize


please choose number or give up 5


真无语了,0~3都不会输入
posted @ 2025-12-17 14:39  LM0T  阅读(4)  评论(0)    收藏  举报