python头歌练习题

第一章

1.calculation and builtin function

1.1第1关:counting the number of characters
# Counting Characters -----------------
# Be sure the output contains the original string.
# Use a single output statement to construct the output.
# Use a built-in function of the programming language to determine the length of the string.

# input----------------------------
input_str = input("What is the input string? ")# Please supplement the code on the left 请在左侧补充代码

# determine the length of the string 
input_str_length =len(input_str) # Please supplement the code on the left 请在左侧补充代码

# print---------------
print(f"{input_str} has {input_str_length} characters.") # Please supplement the code on the left 请在左侧补充代码
1.2 第2关:-Retirement Calculator
# Retirement Calculator --------------
# be sure to convert the input to numerical data before doing any math
# Do not hard-code the current year into your program. Get it from the system time via python language.

# import module
import datetime # Please supplement the code on the left 请在左侧补充代码

# input---------------
age_str = input("What is your current age? ") # Please supplement the code on the left 请在左侧补充代码
age =int(age_str)  # Please supplement the code on the left 请在左侧补充代码
retirement_age_str =input("At what age would you like to retire? ") # Please supplement the code on the left 请在左侧补充代码
retirement_age =int(retirement_age_str) # Please supplement the code on the left 请在左侧补充代码

# processing----------------
# get_current_year
year =datetime.datetime.now().year# Please supplement the code on the left 请在左侧补充代码

# calc_years_till_retirement
years_left =retirement_age-age # Please supplement the code on the left 请在左侧补充代码

# calc_retirement_year
retirement_year =year+years_left # Please supplement the code on the left 请在左侧补充代码

# print----------------
# print_years_left_messages
print(f"You have {years_left} years left until you can retire.") # Please supplement the code on the left 请在左侧补充代码
# print_retirement_year_message
print(f"It's {year}, so you can retire in {retirement_year}.") # Please supplement the code on the left 请在左侧补充代码

1.3第3关:Determining Compound Interest

# Determining Compound Interest-----------
# Prompt for the rate as a percentage (like 15, not .15). Divide the input by 100 in your program.
# Ensure that fractions of a cent are rounded up to the next penny.
# Ensure that the output is formatted as money¥.
import math
# input-------------
principal = float(input('Enter the principal: ')) # Please supplement the code on the left 请在左侧补充代码
rate = float(input('Enter the rate: '))/100 # Please supplement the code on the left 请在左侧补充代码
years = int(input('Enter the number of years: ')) # Please supplement the code on the left 请在左侧补充代码
compound = int(input('Enter the number of times the interest is compounded per year: ')) # Please supplement the code on the left 请在左侧补充代码

# calc_investment
investment_value = principal * math.pow(1 + (rate / compound), compound * years) # Please supplement the code on the left 请在左侧补充代码

# print_investment
print(f'¥{principal:.2f} invested at {rate * 100:.2f}% rate for {years} years compounded {compound} times per year is ¥{investment_value:.2f}.') # Please supplement the code on the left 请在左侧补充代码

2.input,processing and output

2.1第1关 simple math
# Simple math--------------------
# Ensure that you convert these values to int numbers before doing the math.
# Keep the inputs and outputs separate from the numerical conversions and other processing.
# Generate a single output statement with line breaks in the appropriate spots.

# input------------------
first_number_str =input('What is the first number? ') # Please supplement the code on the left 请在左侧补充代码
first_number =int(first_number_str) # Please supplement the code on the left 请在左侧补充代码
second_number_str =input('What is the second number? ') # Please supplement the code on the left 请在左侧补充代码
second_number =int(second_number_str) # Please supplement the code on the left 请在左侧补充代码

# add_numbers
add_numbers_result =first_number+second_number# Please supplement the code on the left 请在左侧补充代码

# subtract_numbers
subtract_numbers_result =first_number-second_number # Please supplement the code on the left 请在左侧补充代码

# multiply_numbers
multiply_numbers_result =first_number*second_number # Please supplement the code on the left 请在左侧补充代码

# divide_numbers
divide_numbers_result =first_number/second_number # Please supplement the code on the left 请在左侧补充代码

# print----------------
# print_add_numbers_result
print(f'{first_number} + {second_number} = {add_numbers_result}')# Please supplement the code on the left 请在左侧补充代码

# print_subtract_numbers_result
print(f'{first_number} - {second_number} = {subtract_numbers_result}')# Please supplement the code on the left 请在左侧补充代码

# print_multiply_numbers_result
print(f'{first_number} * {second_number} = {multiply_numbers_result}') # Please supplement the code on the left 请在左侧补充代码

# print_divide_numbers_result
print(f'{first_number} / {second_number} = {divide_numbers_result}') # Please supplement the code on the left 请在左侧补充代码

2.2第2关 say hello
# Saying Hello ------------
# Keep the input, string concatenation, and output separate.

# input----------
name = input("What is your name? ") # Please supplement the code on the left 请在左侧补充代码

# string concatenation-------
say = "Hello" # Please supplement the code on the left 请在左侧补充代码

# output---------
print(f"{say}, {name}, nice to meet you!") # Please supplement the code on the left 请在左侧补充代码
 
2.3第3关 printing quotes
# Printing Quotes -------------
# Use a single output statement to produce this output, using appropriate quotation marks for quotes.
# Use string interpolation or string substitution for this exercise. 

# input--------------------
quote = input("What is the quote? ")# Please supplement the code on the left 请在左侧补充代码
author =input("Who said it? ") # Please supplement the code on the left 请在左侧补充代码

# print--------------------
print(f"{author} says, \"{quote}\"") # Please supplement the code on the left 请在左侧补充代码

3.Basic Knowledge training

3.1第1关 Calculation of individual income tax(initial version)
# Tax calculator -----------------
# All taxpayers are charged a flat tax rate of 20%.
# All taxpayers are allowed a ¥50,000 standard deduction.
# For each dependent, a taxpayer is allowed an additional ¥1000 deduction.
# The Gross income entered must be an integer in Chinese Yuan (RMB).
# The income tax is expressed as a decimal number.

# Initialize the constants

gross_income = float(input("Enter the gross income: "))
dependents = int(input("Enter the number of dependents: "))

# Compute the income tax
taxable_income = gross_income - 5000 - (1000 * dependents)
tax = taxable_income * 0.20
# Display the income tax
print(f"The income tax is ¥{tax:.2f}.")
3.2第2关 Calculation of individual income tax(optimize)
# Tax calculator -----------------
# All taxpayers are charged a flat tax rate of 20%.
# All taxpayers are allowed a ¥50,000 standard deduction.
# For each dependent, a taxpayer is allowed an additional ¥1000 deduction.
# The Gross income entered must be an integer in Chinese Yuan (RMB).
# The income tax is expressed as a decimal number.

# Initialize the constants
num_of_dependents = 0  # 初始化被抚养人数
gross_income = 0  # 初始化总收入
deduction = 0  # 初始化扣除额
taxable_income = 0  # 初始化应纳税所得额
income_tax = 0  # 初始化所得税

# Request the inputs
gross_income = float(input("Enter the gross income: "))
dependents = int(input("Enter the number of dependents: "))

# Compute the income tax
taxable_income = gross_income - 5000 - (1000 * dependents)

# 步骤 3:判断应税收入是否大于 0
if taxable_income > 0:
    # 步骤 3.1:如果大于 0,使用公式计算所得税
    tax = taxable_income * 0.20
else:
    # 步骤 3.2:如果不大于 0,将所得税设置为 0
    tax = 0
# Display the income tax
print(f"The income tax is ¥{tax:.2f}.")
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值