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
有點羅嗦,不過以後才改吧,畢竟剛學。