python--through a while, for, If-else to complete a subway car consumption calculator __python

Source: Internet
Author: User
Tags ming
python--completes a small program through a while, for, If-else

On the third day of learning Python, Mr. Zheng Let us write a calculation of the fare of the small program, fortunately before there is a small program to write the basis, and then soon completed, the following is related to the content (^_^) ∠※ topic Requirements:

according to the following information, please help Xiao Ming calculate the total cost of his monthly subway expenses:
Rail Transit price adjustment for: 6 km (including) within 3 yuan, 6 km to 12 km (including) 4 yuan, 12 km to 22 km (including) 5 yuan; 22 km to 32 km (including) 6 yuan, 32 kilometers above the section, each additional 1 yuan can take 20 kilometers. The use of municipal traffic card by rail traffic, every natural month, each card expenditure accumulated 100 yuan after the times, the price to give 80 percent discount, 150 yuan after the times, the price to give 50 percent discount, the total expenditure reached 400 yuan after the times, no longer enjoy discounts.
Requirements:
Suppose that every month, Xiaoming needs 20 days, every time to go back and forth 1 times, that is, the daily need to take 2 times the same route of the subway; Write a program to help Xiao Ming complete the total cost of taking the subway every month .

Here is the code implementation:

#-*-coding:utf-8-*-Print ("* * * Subway car consumption calculation Calculator * * * * * * * * * * * * * * * * * * * distance = input (" Please enter your travel distance greater than 0: ") #输入每天去公司乘坐地铁的距离 (km) if di Stance ==0:print ("Stay where you Are") exit () cost = 0.0 #设置变量cost记录用户在地铁上的消费情况 (yuan                                      Day = 1 #设置变量记录天数 #while day<=20:while day<=40:                                           #共20天, 40 trips back and forth rate = 0.0 #设置优惠率 # i = 1 #设置while循环, the number of days is greater than 20 o'clock end loop # while i<=2: #设置内嵌while循环, calculate back and forth consumption separately
        , the same ride back and forth consumption may not be the same if cost>=100 and cost<150: #设置if-elif-else statement to determine the current consumption, and set the variable record discount rate                                 Rate = 0.8 Elif cost>=150 and cost<400:rate = 0.5 Else:rate = 1.0 if distance<=6: #使用if-elif-else statement to judge the user's travel range, at the same time increase the corresponding cost in total consumption cost+= (3*rate) elif distance>
 6 and Distance<=12:       cost+= (4*rate) elif distance>12 and distance<=22:cost+= (5*rate) elif distance>22 and Dist ance<=32:cost+= (6*rate) Else: #乘车超过32公里时候的计算公式 if (dista 
    NCE-32)%20==0:cost+= ((6+ (distance-32)/20) *rate) else:cost+= ((6+ (distance-32)/20+1) *rate) Day+=1 print ("You spend the month on the subway for%.2f"%cost) #输出花费 (yuan), keep two decimal places

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.