现在的位置: 首页 > 综合 > 正文

python 学习记录(4)—本金+利息计算及简单的游戏猜测程序

2013年08月31日 ⁄ 综合 ⁄ 共 3139字 ⁄ 字号 评论关闭

1、下面的程序是当时学习python时想到最近降息,利率变化了写的简单计算本息和的代码,很简单,只为巩固当时学的内容,现在记录在这里供分享和日后查找相关用法

# filename:interest_calculation.py
print '''hello,welcome to use this interest calculation program \n'''
principal = int (raw_input ('please enter your interest (it must be a integer ):') )
print '''then choose how long you will be save the money based on the rate \n '''
print "CURRENT\n the current interest is 0.005 \n "
print "STATED\n the stated interest are as following: \n "
print " 0.031 for three month \n 0.033 for half year \n \
0.035 for one year \n 0.044 for two year \n "

#user input the rate
rate  = float(raw_input ('please enter one of above rate :') )
#or based on the time ,you can use numtimes = int(raw_input ('please enter the  saving time (it should  be above one  ):') )

#calculate the expected interest
principal = principal * (1 + rate )

#judge which interest should be the output
#the following case use the function format(), you can reference the helps to know more about the detail

##########  reference .format #############\
#1   Substitute positional argument 0 into the string.
#   "User ID: {0}".format("root") -> "User ID: root"
#2   Use the named keyword arguments
#   'User ID: {uid} Last seen: {last_login}'.format(uid='root', last_login = '5 Mar 2008 07:20') ->'User ID: root Last seen: 5 Mar 2008 07:20'

if rate  == 0.005:
    print "the rate is {0} and the interest will be {1:0.3f}" .format(rate, principal)  
elif rate  ==0.031:
    print "the rate is {0} and the interest will be {1:0.3f}" .format(rate, principal)
elif rate  ==0.033:
    print "the rate is {0} and the interest will be {1:0.3f}" .format(rate, principal)
elif rate  ==0.035:
    print "the rate is {0} and the interest will be {1:0.3f}" .format(rate, principal)
elif rate  ==0.044:
    print "the rate is {0} and the interest will be {1:0.3f}" .format(rate, principal)
else :
    print "type error, the input does not match to the rate !\n \
    please input the right rate again !"
以上用不同颜色标注的应该就是知识点吧!

  

2、下面的这个是根据公司羽毛球比赛“章鱼哥”猜猜看的游戏写的,还没有写完(需要自动判断最后猜对的人员并将结果记录保存,最后随着比赛的深入,还有统计各个参加者的猜对次数等),但是基本能输出谁赢了。

#filename:game_guess
#coding=utf-8
player_name1 = (raw_input ('''please enter the first player's NAME:''' ) )
player_name2 = (raw_input ('''please enter the second player's NAME:''' ) )
print '''player names recorded success and it has been saved \n '''

print "please input the scores under the following blanks \n "
score_1 = int (raw_input ('''please enter the first player's SCORE:''' ) )
score_2 = int (raw_input ('''please enter the second player's SCORE:''' ) )

who_plus = (raw_input (''' enter the  plus_score player's NAME:''' ) )

plus_score = int (raw_input (''' enter the PLUS_SCORE(must be an integer):''' ) )

if who_plus == player_name1
:

    score_1 = score_1 + plus_score
elif who_plus == player_name2:
    score_2 =  score_2 + plus_score
else:
    print "the input player name is not correspond, please try again from the beginning"    
     
if score_1 > score_2 :
    print ''' the guess win player is {0}, the guess score is {1} ''' .format(player_name1, score_1)
elif score_1 < score_2 :
    print ''' the guess win player is {0}, the guess score is {1} ''' .format(player_name2, score_2)
else:
    print "the score is the same, cannot tell the winner "
以上用不同颜色标注的应该就是知识点吧!

输出:

please enter the first player's NAME:qwe
please enter the second player's NAME:asd
player names recorded success and it has been saved
 
please input the scores under the following blanks
 
please enter the first player's SCORE:50
please enter the second player's SCORE:30
 enter the  plus_score player's NAME:asd
 enter the PLUS_SCORE(must be an integer):25
 the guess win player is asd, the guess score is 55

有点罗嗦,不过以后才改吧,毕竟刚学。

抱歉!评论已关闭.