2020-5-8 Python基础第一模块练习
from time import *
#------------------------求质数之和-------------------
x=1
sum=0
while x<100 and x%2 !=0 :
x+=2
sum +=x
else :
print('finished')
print(sum)
#-------------求100以内所有7的倍数之和,以及个数---------------------
a=7
sum=7
count=0
while a<100 and a%7 ==0:
sum+=a
count+=1
print(a)
a += 7
else:
print('finised')
print('和为:',sum)
print('个数为:',count)
#--------------------水仙花数-------------------------
x=100
while x<1000:
a=x//100
b=x//10%10
#b=(x-a*100)//10
c=x%10
if a**3+b**3+c**3 ==x :
print(x)
x+=1
#--------------------------判断是否为质数----------------------------
num=int(input('输入一个任意大于1的数:'))
a=2
flag=True
while a<num:
if num%a==0:
flag=False
a+=1
if flag:
print(num,"是质数")
else:
print('不是质数')
#-------打印5*5星号图形----------------------------------
i=0
while i<5:
j=5
while i<j:
print('*',end='')
j-=1
print()
i+=1
#----------打印9*9乘法表-----------------------------------
i=0
while i<9:
i+=1
j=0
while j<i:
j+=1
print(f'{j}*{i} ',end='')
print()
#--------100以内所有质数--------------------------------------
begin = time()
x=2
while x<=100:
flag=True
i=2
while x>i:
if x%i==0 :
flag=False
break
i+=1
if flag:
print(x)
x+=1
end= time()
print('time=',end-begin,'s')
#-------------------小游戏开发------------------------------------
#----游戏欢迎信息--------
print('-'*20,'welcome to this little game','-'*20)
print('please choose your indentity:')
print('\t1.monk')
print('\t2.white bone demon')
player_choose=input('请选择[1-2]:')
print('-'*100)
if player_choose=='1':
print('you have chosen monk, which is your identity to play game')
elif player_choose=='2':
print('you have chosen demon, which is too impractical. So you will be automatically designed to monk')
else:
print('your choose is wrong, and your will be assigned to monk to play the game ')
#------进入游戏----------
player_life=2
player_attack=2
boss_life=10
boss_attack=10
print(f'monk,your life value is {player_life}, and your attach value is {player_attack}')
while True:
print('-' * 100)
print('please choose your upcoming operation:')
print('\t1. leveling')
print('\t2. fight boss')
print('\t3. run away')
game_choose=input('please chose your operation[1-3]:')
if game_choose=='1':
player_life+=2
player_attack+=2
print('-' * 100)
print(f'woo, congratulations! Monk,your life value is {player_life}, and your attach value is {player_attack}')
elif game_choose =='2':
boss_life-=player_attack
if boss_life<=0:
print('you win!!!')
break
player_life-=boss_attack
print('-' * 100)
print('you are hurt')
if player_life<=0:
print('game over')
print('-' * 100)
break
elif game_choose=='3':
print('-' * 100)
print('monk is so scared, game over....')
print('-' * 100)
break
else:
print('-' * 100)
print('your input is wrong, please retype your choose.')
print('-' * 100)