python语言程序设计_梁勇—第二章习题练习答案

将摄氏温度转化为华氏温度

import math
def temperture_convertion():
    celsius = eval(input("输入摄氏温度:"))
    fahrenheit = (9/5)*celsius+32
    print("华氏温度为:",fahrenheit)

计算圆柱体的面积

def calculate_the_area_of_cylinder():
    radius,length = eval(input("Enter the radius and length of a culinder:"))
    area = radius * radius * math.pi
    volume = area * length
    print("The area is %.2f"%area)
    print("The volume is %.2f"%volume)

对一个整数中的各位数字求和

def summation():
    number = eval(input("输入一个1-1000之间的整数:"))
    single_digits = number % 10
    ten_digits = number // 10 % 10
    percentile = number //100
    sum = single_digits + ten_digits + percentile
    print("The sum of the digits is %d"%sum)

计算年数和天数

def number_of_year_and_days():
    input_minutes = eval(input("Enter the number of minutes:"))
    days = input_minutes//(60*24) % 365
    years = input_minutes//(60*24)//365
    print("%d minutes is approcimately %d years and %d days"%(input_minutes,years,days))

计算能量

def calculate_power():
    amount_of_water = eval(input("Enter the amount of water in kilograms:"))
    initial_temputure = eval(input("Enter the initial temputure:"))
    final_temputure = eval(input("Enter the final tempure:"))
    Q = amount_of_water *(final_temputure - initial_temputure) * 4184
    print("The energy needed is %.1f"%Q)

金融应用程序

def calculate_investment():
    account = eval(input("Enter the account value:"))
    interest_rate = eval(input("Enter annual interest rate in percent:"))
    number_of_years = eval(input("Enter the number of years:"))
    initial_deposit = account/(1+interest_rate/100)**(number_of_years*12)
    print("Initial deposit value is %f"%initial_deposit)

提示用户输入四位数并反向显示

def reverse_display_figures():
    num = str(input("Enter an integer:"))
    for i in range(len(num)):
        print(num[-(i+1)],end="\n")

获取当前时间

关键点:可以使用time函数中的time()函数来获取当前的系统时间,time函数返回的是以毫秒为精度的从1970年1月1日0点开始到现在的GMT时间

import time
def show_current_time():
    time_zone = eval(input("Enter the time zone:"))
    current_time = time.time() # 获取当前时间
    total_senconds = int(current_time)
    current_sencond = total_senconds % 60
    total_minutes = total_senconds // 60
    current_minute = total_minutes % 60
    total_hours = total_minutes // 60
    current_hour = total_hours % 24
    china_curent_hour = current_hour + time_zone
    print("current time is ", china_curent_hour,':',current_minute,":",
          current_sencond,'China')

计算未来投资额

def future_investment():
    investment_amount = eval(input("Enter investment amount:"))
    annual_interest_rate = eval(input("Enter annual interest rate:"))
    year = eval(input("Enter number of years:"))
    accumulated_value = investment_amount*(1+annual_interest_rate/100)**year
    print("Accumulated value is %f"%accumulated_value)

金融应用程序 复利值

def compound_interest():
    saving_amount = eval(input("Enter the monthly saving amount:"))
    month_interest = 0.00417
    month = eval(input("Enter the month:"))
    saving_amount_profit = saving_amount*(1+month_interest)
    for i in range(month-1):
        saving_amount_profit = (saving_amount+saving_amount_profit)*(1+month_interest)

人口预测

def projected_population():
    year = eval(input("Enter the number of years:"))
    population = 3120324986
    people_born_every_second = 7
    people_dead_every_second = 13
    new_immigration = 45
    the_time_of_every_year = 365 * 24 * 60 * 60 * year
    annual_population_bored_increase = the_time_of_every_year // people_born_every_second
    annual_population_immigration = the_time_of_every_year // new_immigration
    annual_population_dead = the_time_of_every_year // people_dead_every_second
    annual_population_increase = population + annual_population_bored_increase + annual_population_immigration - annual_population_dead
    the_total_of_5_years_population = annual_population_increase
    print("%d年后的总人口为:%d"%(year,the_total_of_5_years_population))



if __name__ == '__main__':
    projected_population()
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java语言程序设计梁勇第二章 第二章介绍了Java语言的基本语法和语法规则,包括标识符、关键字、数据类型、变量定义、赋值语句、算术运算符、逻辑运算符、位运算符、条件语句、循环语句等。同时还介绍了常见的数组、字符串和输入输出等基本操作。本章的学习是掌握Java语言编程的基础,也是后续多重复杂的编程任务的必备知识。 本章的主要内容包括: 1. 标识符:标识符是用来标识Java程序中各种元素(如类、方法、变量等)的名称。标识符的命名规则和规范都需要遵守,主要包括以下几点: - 标识符必须以字母、下划线或美元符号开头,并且不能以数字开头; - 标识符可以由多个字符组成,包括字母、数字、下划线和美元符号等; - 标识符对大小写敏感,即“abc”和“ABC”是不同的标识符; - 标识符命名应具有描述性,使程序易于阅读和理解。 2. 关键字:Java语言中使用关键字来指定程序的基本结构、数据类型、访问控制等等细节。在程序中使用关键字需要遵守一定的规律,主要包括以下几点: - 所有关键字都是小写字母; - 关键字用于指示Java程序的基本结构、数据类型、访问控制等细节; - 关键字不能用作标识符。 3. 数据类型:Java中有八种基本数据类型,分别为boolean、byte、short、int、long、float、double和char。对于每种基本数据类型,都有相应的取值范围和数据表示。 4. 变量和常量:变量和常量是Java程序中必不可少的组成部分。变量可以用来储存数据,常量则用来指定程序中的固定值。在程序中声明变量和常量需要注意一些规则,如变量必须先声明再使用等。 5. 表达式:在Java程序中,表达式是由操作数和操作符组成的算术、逻辑或位运算式,可以计算出一个最终结果。Java中提供了一些基本的算术、逻辑和位运算符,例如“+”、“-”、“*”、“/”、“%”、“<”、“>”、“||”、“&&”、“|”,“&”等。 6. 控制流语句:Java中提供了很多控制程序流程的语句,如if语句、switch语句、while语句、for语句和do-while语句等。这些语句在程序设计中非常重要,可以实现程序的不同逻辑控制流程。 7. 数组:数组是一种可以储存多个数据的数据结构。Java中使用数组可以储存同一类型的多个元素,并为数组中的每个元素指定一个索引号。Java中的数组可以有多维,即可以是二维、三维等。 8. 字符串操作:字符串操作在Java程序中经常用到,包括字符串的拼接、替换、查找、比较等等。Java中的字符串类型是String类,可以用来创建和操作字符串。 9. 输入输出:Java提供了输入输出操作类,可以从输入流中读取数据,输出流中写入数据,并进行文件读写及网络数据传输等操作。在Java中,可以使用InputStreamReader和BufferedReader等类来实现用户输入的读取,并使用PrintStream和FileWriter等类进行文本输出和文件写入。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值