csci-1100-hw-1
This commit is contained in:
BIN
content/en/posts/csci-1100/hw-1/HW1.zip
Normal file
BIN
content/en/posts/csci-1100/hw-1/HW1.zip
Normal file
Binary file not shown.
315
content/en/posts/csci-1100/hw-1/index.md
Normal file
315
content/en/posts/csci-1100/hw-1/index.md
Normal file
@@ -0,0 +1,315 @@
|
|||||||
|
---
|
||||||
|
title: CSCI 1100 - Homework 1 - Calculations and Strings
|
||||||
|
subtitle:
|
||||||
|
date: 2024-03-12T02:12:11-04:00
|
||||||
|
slug: csci-1100-hw-1
|
||||||
|
draft: false
|
||||||
|
author:
|
||||||
|
name: James
|
||||||
|
link: https://www.jamesflare.com
|
||||||
|
email:
|
||||||
|
avatar: /site-logo.avif
|
||||||
|
description: This blog post provides a detailed overview of a Python programming homework assignment, which includes creating a Mad Libs game, calculating speed and pace, and generating a framed box with user-specified dimensions.
|
||||||
|
keywords: ["Python", "programming", "homework", "Mad Libs", "speed calculation", "framed box"]
|
||||||
|
license:
|
||||||
|
comment: true
|
||||||
|
weight: 0
|
||||||
|
tags:
|
||||||
|
- CSCI 1100
|
||||||
|
- Homework
|
||||||
|
- RPI
|
||||||
|
- Python
|
||||||
|
- Programming
|
||||||
|
categories:
|
||||||
|
- CSCI
|
||||||
|
- Programming
|
||||||
|
hiddenFromHomePage: false
|
||||||
|
hiddenFromSearch: false
|
||||||
|
hiddenFromRss: false
|
||||||
|
hiddenFromRelated: false
|
||||||
|
summary: This blog post provides a detailed overview of a Python programming homework assignment, which includes creating a Mad Libs game, calculating speed and pace, and generating a framed box with user-specified dimensions.
|
||||||
|
resources:
|
||||||
|
- name: featured-image
|
||||||
|
src: featured-image.jpg
|
||||||
|
- name: featured-image-preview
|
||||||
|
src: featured-image-preview.jpg
|
||||||
|
toc: true
|
||||||
|
math: false
|
||||||
|
lightgallery: false
|
||||||
|
password:
|
||||||
|
message:
|
||||||
|
repost:
|
||||||
|
enable: true
|
||||||
|
url:
|
||||||
|
|
||||||
|
# See details front matter: https://fixit.lruihao.cn/documentation/content-management/introduction/#front-matter
|
||||||
|
---
|
||||||
|
|
||||||
|
<!--more-->
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
This homework, worth 100 points total toward your overall homework grade, is due Thursday, September 14, 2023 at 11:59:59 pm. The three parts should each be submitted separately. All parts should be submitted by the deadline or your program will be considered late.
|
||||||
|
|
||||||
|
Please refer to the Submission Guidelines document before starting this assignment. It will give you details on what we expect and will answer some of the more common questions, including that you need to submit your program through Submitty and that Submitty will open by Monday, September 11th, 2023.
|
||||||
|
|
||||||
|
Remember that your output must match EXACTLY the format shown in example runs from the `hw01_files.zip` file. The purpose of this is to make testing easier and at the same time teach you how to be precise in your programming using the tools we gave you. We love creativity, but not in HW output formatting!
|
||||||
|
|
||||||
|
## Part 1: Mad Libs (40 pts)
|
||||||
|
|
||||||
|
In this part, you will write a Python program to construct the Mad Lib given below:
|
||||||
|
|
||||||
|
```text
|
||||||
|
Good morning <proper name>!
|
||||||
|
|
||||||
|
This will be a/an <adjective1> <noun1>! Are you <verb1> forward to it?
|
||||||
|
You will <verb2> a lot of <noun2> and feel <emotion1> when you do.
|
||||||
|
If you do not, you will <verb3> this <noun3>.
|
||||||
|
|
||||||
|
This <season> was <adjective2>. Were you <emotion2> when <team_name> won
|
||||||
|
the <noun4>?
|
||||||
|
|
||||||
|
Have a/an <adjective3> day!
|
||||||
|
```
|
||||||
|
|
||||||
|
You will ask the user of the program for the missing words | those enclosed in `< >` | using the input function. You will then take all the user-specified inputs and construct the above Mad Lib. Make sure your output looks like the above paragraph, except that the missing information is filled in with the user input.
|
||||||
|
|
||||||
|
An example of the program run is provided in file `hw1 part1 output.txt` (You will need to download file `hw01_files.zip` from the Course Materials section of Submitty and unzip it into your directory for HW 1).
|
||||||
|
|
||||||
|
We've provided reasonable inputs, but the idea of Mad Libs is to input random words and see how silly the result looks. Try it!
|
||||||
|
|
||||||
|
Of course, the program you write will only work for the specific Mad Lib we've written above. A more challenging problem, which you will be capable of solving by the end of the semester, is to write a program that reads in any Mad Lib, figures out what to ask the user, asks the user, reads the input, and generates the final Mad Lib.
|
||||||
|
|
||||||
|
Test your code well and when you are sure that it works, please submit it as a file named `hw1 part1.py` to Submitty for Part 1 of the homework.
|
||||||
|
|
||||||
|
## Part 2: Speed Calculations (40 pts)
|
||||||
|
|
||||||
|
Many exercise apps record both the time and the distance a user covers while walking, running, biking, or swimming. Some users of the apps want to know their average pace in minutes and seconds per mile, while others want to know their average speed in miles per hour. In many cases, we are interested in projected time over a specific distance. For example, if I run 6.3 miles in 53 minutes and 30 seconds, my average pace is 8 minutes and 29 seconds per mile, my average speed is 7.07 miles per hour, and my projected time for 2.7 miles is 22 minutes and 55 seconds.
|
||||||
|
|
||||||
|
Your job in Part 2 of this homework is to write a program that asks the user for the minutes, seconds, miles run, and miles to target from an exercise event and outputs both the average pace and the average speed.
|
||||||
|
|
||||||
|
An example of the program run is provided in file `hw1 part2 output.txt` (Can be found inside the `hw01_files.zip` file).
|
||||||
|
|
||||||
|
You can expect minutes and seconds to both be integers, but miles and miles to target will be floats. All minutes and seconds must be maintained as integers so please use integer division and modulo operations. For example:
|
||||||
|
|
||||||
|
```python
|
||||||
|
>>> x = 29.52
|
||||||
|
>>> y = int(x)
|
||||||
|
>>> print(y)
|
||||||
|
29
|
||||||
|
```
|
||||||
|
|
||||||
|
The output for the speed will be a float and should be printed to 2 decimal places. Notice also that our solution generates a blank line before the output of calculations.
|
||||||
|
|
||||||
|
We will test your code for the values used in our examples as well as a range of different values. Test your code well and when you are sure that it works, please submit it as a file named `hw1 part2.py` to Submitty for Part 2 of the homework.
|
||||||
|
|
||||||
|
## Part 3: Framed Box (20 pts)
|
||||||
|
|
||||||
|
Write a program that asks the user for a frame character, and then the height and width of a framed box. Then output a box of the given size, framed by the given character. Also, output the dimensions of the box centered horizontally and vertically inside the box. In case perfect vertical centering is not possible, dimensions should be output such that there is one less row above the text than below it. In case perfect horizontal centering is not possible, dimensions should be output such that there is one less space character to the left of the text than to the right.
|
||||||
|
|
||||||
|
Assume that the user inputs valid values for each input: width is a positive integer (7 or higher) and height is a positive integer (4 or higher), and a single character is given for the frame.
|
||||||
|
|
||||||
|
Two examples of the program run are provided in files `hw1 part3 output 01.txt` and `hw1 part3 output 02.txt` (Can be found inside the `hw01_files.zip` file).
|
||||||
|
|
||||||
|
You will need to put the box dimensions in a string first, and then use its length to figure out how long the line containing the dimensions should be. If you have prior programming experience you might be tempted to look for how Python implements "loops" in order to generate the full frame, but Python provides string manipulation tools (Lecture 3) that make this unnecessary. You must not use any if statements or loops in your program. We have not learned them yet, they are not needed, and they will not make your solution better or more elegant.
|
||||||
|
|
||||||
|
We will test your code for the values used in our examples as well as a range of different values. Test your code well and when you are sure that it works, please submit it as a file named `hw1 part3.py` to Submitty for Part 3 of the homework.
|
||||||
|
|
||||||
|
## Supporting Files
|
||||||
|
|
||||||
|
{{< link href="HW1.zip" content="HW1.zip" title="Download HW1.zip" download="HW1.zip" card=true >}}
|
||||||
|
|
||||||
|
***
|
||||||
|
|
||||||
|
## Solution
|
||||||
|
|
||||||
|
### hw1_part1.py
|
||||||
|
|
||||||
|
```python
|
||||||
|
#Prepare variables
|
||||||
|
|
||||||
|
proper_name = ""
|
||||||
|
adjective1 = ""
|
||||||
|
noun1 = ""
|
||||||
|
verb1 = ""
|
||||||
|
verb2 = ""
|
||||||
|
noun2 = ""
|
||||||
|
emotion1 = ""
|
||||||
|
verb3 = ""
|
||||||
|
noun3 = ""
|
||||||
|
season = ""
|
||||||
|
adjective2 = ""
|
||||||
|
emotion2 = ""
|
||||||
|
team_name = ""
|
||||||
|
noun4 = ""
|
||||||
|
adjective3 = ""
|
||||||
|
|
||||||
|
template= """
|
||||||
|
Good morning <proper name>!
|
||||||
|
|
||||||
|
This will be a/an <adjective1> <noun1>! Are you <verb1> forward to it?
|
||||||
|
You will <verb2> a lot of <noun2> and feel <emotion1> when you do.
|
||||||
|
If you do not, you will <verb3> this <noun3>.
|
||||||
|
|
||||||
|
This <season> was <adjective2>. Were you <emotion2> when <team_name> won
|
||||||
|
the <noun4>?
|
||||||
|
|
||||||
|
Have a/an <adjective3> day!"""
|
||||||
|
output = ""
|
||||||
|
|
||||||
|
#Get user's input
|
||||||
|
|
||||||
|
print("Let's play Mad Libs for Homework 1")
|
||||||
|
print("Type one word responses to the following:\n")
|
||||||
|
|
||||||
|
proper_name = input("proper_name ==> ").strip()
|
||||||
|
print(proper_name)
|
||||||
|
adjective1 = input("adjective ==> ").strip()
|
||||||
|
print(adjective1)
|
||||||
|
noun1 = input("noun ==> ").strip()
|
||||||
|
print(noun1)
|
||||||
|
verb1 = input("verb ==> ").strip()
|
||||||
|
print(verb1)
|
||||||
|
verb2 = input("verb ==> ").strip()
|
||||||
|
print(verb2)
|
||||||
|
noun2 = input("noun ==> ").strip()
|
||||||
|
print(noun2)
|
||||||
|
emotion1 = input("emotion ==> ").strip()
|
||||||
|
print(emotion1)
|
||||||
|
verb3 = input("verb ==> ").strip()
|
||||||
|
print(verb3)
|
||||||
|
noun3 = input("noun ==> ").strip()
|
||||||
|
print(noun3)
|
||||||
|
season = input("season ==> ").strip()
|
||||||
|
print(season)
|
||||||
|
adjective2 = input("adjective ==> ").strip()
|
||||||
|
print(adjective2)
|
||||||
|
emotion2 = input("emotion ==> ").strip()
|
||||||
|
print(emotion2)
|
||||||
|
team_name = input("team-name ==> ").strip()
|
||||||
|
print(team_name)
|
||||||
|
noun4 = input("noun ==> ").strip()
|
||||||
|
print(noun4)
|
||||||
|
adjective3 = input("adjective ==> ").strip()
|
||||||
|
print(adjective3)
|
||||||
|
|
||||||
|
#Construct the Mad Lib
|
||||||
|
|
||||||
|
output = template.replace("<proper name>", proper_name)
|
||||||
|
output = output.replace("<adjective1>", adjective1)
|
||||||
|
output = output.replace("<noun1>", noun1)
|
||||||
|
output = output.replace("<verb1>", verb1)
|
||||||
|
output = output.replace("<verb2>", verb2)
|
||||||
|
output = output.replace("<noun2>", noun2)
|
||||||
|
output = output.replace("<emotion1>", emotion1)
|
||||||
|
output = output.replace("<verb3>", verb3)
|
||||||
|
output = output.replace("<noun3>", noun3)
|
||||||
|
output = output.replace("<season>", season)
|
||||||
|
output = output.replace("<adjective2>", adjective2)
|
||||||
|
output = output.replace("<emotion2>", emotion2)
|
||||||
|
output = output.replace("<team_name>", team_name)
|
||||||
|
output = output.replace("<noun4>", noun4)
|
||||||
|
output = output.replace("<adjective3>", adjective3)
|
||||||
|
|
||||||
|
#Print the Mad Lib
|
||||||
|
|
||||||
|
print("\nHere is your Mad Lib...")
|
||||||
|
print(output, end="")
|
||||||
|
```
|
||||||
|
|
||||||
|
### hw1_part2.py
|
||||||
|
|
||||||
|
```python
|
||||||
|
#Perpare Variables
|
||||||
|
|
||||||
|
minutes = 00
|
||||||
|
seconds = 00
|
||||||
|
miles = 00.00
|
||||||
|
target_miles = 00.00
|
||||||
|
|
||||||
|
pace_seconds_per_mile = 00.00
|
||||||
|
pace_seconds = 00
|
||||||
|
pace_minutes = 00
|
||||||
|
|
||||||
|
speed_mph = 00.00
|
||||||
|
|
||||||
|
target_time_total_seconds = 00.00
|
||||||
|
target_time_seconds = 00
|
||||||
|
target_time_minutes = 00
|
||||||
|
|
||||||
|
#Get User Input
|
||||||
|
|
||||||
|
minutes = str(input("Minutes ==> "))
|
||||||
|
print(minutes)
|
||||||
|
seconds = str(input("Seconds ==> "))
|
||||||
|
print(seconds)
|
||||||
|
miles = str(input("Miles ==> "))
|
||||||
|
print(miles)
|
||||||
|
target_miles = str(input("Target Miles ==> "))
|
||||||
|
print(target_miles)
|
||||||
|
|
||||||
|
#Calculate Pace
|
||||||
|
|
||||||
|
pace_seconds_per_mile = (int(minutes) * 60 + int(seconds)) / float(miles)
|
||||||
|
pace_seconds = int(pace_seconds_per_mile % 60)
|
||||||
|
pace_minutes = int(pace_seconds_per_mile // 60)
|
||||||
|
|
||||||
|
#Calculate Speed
|
||||||
|
|
||||||
|
speed_mph = float(miles) / (int(minutes) / 60 + int(seconds) / 3600)
|
||||||
|
|
||||||
|
#Calculate Target Time
|
||||||
|
|
||||||
|
target_time_total_seconds = float(target_miles) * pace_seconds_per_mile
|
||||||
|
target_time_seconds = int(target_time_total_seconds % 60)
|
||||||
|
target_time_minutes = int(target_time_total_seconds // 60)
|
||||||
|
|
||||||
|
#Print Results
|
||||||
|
|
||||||
|
print("\nPace is " + str(pace_minutes) + " minutes and " + str(pace_seconds) + " seconds per mile.")
|
||||||
|
print("Speed is {0:.2f} miles per hour.".format(float(speed_mph)))
|
||||||
|
print("Time to run the target distance of {0:.2f} miles is {1} minutes and {2} seconds.".format(float(target_miles), int(target_time_minutes), int(target_time_seconds)), end="")
|
||||||
|
```
|
||||||
|
|
||||||
|
### hw1_part3.py
|
||||||
|
|
||||||
|
```python
|
||||||
|
#Prepare Variables
|
||||||
|
frame_character = ""
|
||||||
|
height = 0
|
||||||
|
width = 0
|
||||||
|
free_space = 0.0
|
||||||
|
|
||||||
|
#Get user input
|
||||||
|
frame_character = input("Enter frame character ==> ").strip()
|
||||||
|
print(frame_character)
|
||||||
|
height = int(input("Height of box ==> ").strip())
|
||||||
|
print(height)
|
||||||
|
width = int(input("Width of box ==> ").strip())
|
||||||
|
print(width, "\n")
|
||||||
|
|
||||||
|
#Calculate dimensions line
|
||||||
|
dimensions = str(width) + "x" + str(height)
|
||||||
|
free_space = width - 2 - len(dimensions)
|
||||||
|
|
||||||
|
#Calculate the left and right padding considering odd/even width
|
||||||
|
left_space = free_space // 2
|
||||||
|
right_space = free_space // 2 + (free_space % 2)
|
||||||
|
|
||||||
|
#Prepare rows
|
||||||
|
top_bottom_row = frame_character * width
|
||||||
|
empty_row = frame_character + " " * (width - 2) + frame_character
|
||||||
|
dimension_row = frame_character + " " * left_space + dimensions + " " * right_space + frame_character
|
||||||
|
|
||||||
|
#Calculate the number of rows before and after the dimensions row
|
||||||
|
before_rows = ((height - 2) // 2) - ((height - 1) % 2)
|
||||||
|
after_rows = height - 3 - before_rows
|
||||||
|
|
||||||
|
#Output box
|
||||||
|
print("Box:")
|
||||||
|
print(top_bottom_row)
|
||||||
|
print((empty_row + '\n') * before_rows, end="")
|
||||||
|
print(dimension_row)
|
||||||
|
print((empty_row + '\n') * after_rows, end="")
|
||||||
|
print(top_bottom_row)
|
||||||
|
```
|
||||||
BIN
content/zh-cn/posts/csci-1100/hw-1/HW1.zip
Normal file
BIN
content/zh-cn/posts/csci-1100/hw-1/HW1.zip
Normal file
Binary file not shown.
315
content/zh-cn/posts/csci-1100/hw-1/index.md
Normal file
315
content/zh-cn/posts/csci-1100/hw-1/index.md
Normal file
@@ -0,0 +1,315 @@
|
|||||||
|
---
|
||||||
|
title: CSCI 1100 - 作业 1 - 计算与字符串处理
|
||||||
|
subtitle:
|
||||||
|
date: 2024-03-12T02:12:11-04:00
|
||||||
|
slug: csci-1100-hw-1
|
||||||
|
draft: false
|
||||||
|
author:
|
||||||
|
name: James
|
||||||
|
link: https://www.jamesflare.com
|
||||||
|
email:
|
||||||
|
avatar: /site-logo.avif
|
||||||
|
description: 这篇博客详细介绍了一项 Python 编程作业,包括创建一个 Mad Libs 文字游戏,计算速度和配速,以及生成一个用户指定尺寸的带框文本框。
|
||||||
|
keywords: ["Python", "编程", "作业", "Mad Libs", "速度计算", "带框文本框"]
|
||||||
|
license:
|
||||||
|
comment: true
|
||||||
|
weight: 0
|
||||||
|
tags:
|
||||||
|
- CSCI 1100
|
||||||
|
- 作业
|
||||||
|
- RPI
|
||||||
|
- Python
|
||||||
|
- 编程
|
||||||
|
categories:
|
||||||
|
- CSCI
|
||||||
|
- 编程语言
|
||||||
|
hiddenFromHomePage: false
|
||||||
|
hiddenFromSearch: false
|
||||||
|
hiddenFromRss: false
|
||||||
|
hiddenFromRelated: false
|
||||||
|
summary: 这篇博客详细介绍了一项 Python 编程作业,包括创建一个 Mad Libs 文字游戏,计算速度和配速,以及生成一个用户指定尺寸的带框文本框。
|
||||||
|
resources:
|
||||||
|
- name: featured-image
|
||||||
|
src: featured-image.jpg
|
||||||
|
- name: featured-image-preview
|
||||||
|
src: featured-image-preview.jpg
|
||||||
|
toc: true
|
||||||
|
math: false
|
||||||
|
lightgallery: false
|
||||||
|
password:
|
||||||
|
message:
|
||||||
|
repost:
|
||||||
|
enable: true
|
||||||
|
url:
|
||||||
|
|
||||||
|
# 查看详细的 front matter 设置: https://fixit.lruihao.cn/documentation/content-management/introduction/#front-matter
|
||||||
|
---
|
||||||
|
|
||||||
|
<!--more-->
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
本次作业总分为100分,将计入你的总作业成绩,截止日期为2023年9月14日 ( 星期四 ) 晚上11:59:59。三个部分需分别提交。所有部分必须在截止日期前提交,否则你的程序将被视为迟交。
|
||||||
|
|
||||||
|
在开始本次作业之前,请仔细阅读提交指南文档。它将详细说明我们的期望,并回答一些常见问题,包括你需要通过 Submitty 提交程序,以及 Submitty 将在2023年9月11日 ( 星期一 ) 之前开放。
|
||||||
|
|
||||||
|
请记住,你的输出格式必须与 `hw01_files.zip` 文件中的示例运行结果完全一致。这样做的目的是简化测试过程,同时教会你如何使用我们提供的工具进行精确编程。我们欣赏创造力,但不要体现在作业的输出格式上!
|
||||||
|
|
||||||
|
## 第1部分:Mad Libs 文字游戏 ( 40分 )
|
||||||
|
|
||||||
|
在这一部分,你将编写一个 Python 程序来构建下面的 Mad Libs 文字游戏:
|
||||||
|
|
||||||
|
```text
|
||||||
|
Good morning <proper name>!
|
||||||
|
|
||||||
|
This will be a/an <adjective1> <noun1>! Are you <verb1> forward to it?
|
||||||
|
You will <verb2> a lot of <noun2> and feel <emotion1> when you do.
|
||||||
|
If you do not, you will <verb3> this <noun3>.
|
||||||
|
|
||||||
|
This <season> was <adjective2>. Were you <emotion2> when <team_name> won
|
||||||
|
the <noun4>?
|
||||||
|
|
||||||
|
Have a/an <adjective3> day!
|
||||||
|
```
|
||||||
|
|
||||||
|
你需要使用 `input` 函数请求程序用户提供缺失的单词,即那些用 `< >` 括起来的单词。然后,你将获取所有用户指定的输入,并构建上面的 Mad Libs 文字游戏。确保你的输出看起来像上面的段落,只是缺失的信息被用户输入所填充。
|
||||||
|
|
||||||
|
文件 `hw1 part1 output.txt` 中提供了程序运行的示例 ( 你需要从 Submitty 的课程材料部分下载 `hw01_files.zip` 文件,并将其解压到你的作业1目录中 )。
|
||||||
|
|
||||||
|
我们提供了合理的输入示例,但 Mad Libs 的乐趣在于输入随机的词,看看结果有多搞笑。不妨试试看!
|
||||||
|
|
||||||
|
当然,你编写的程序只适用于我们上面提供的特定 Mad Libs 文本。一个更具挑战性的问题是,编写一个程序来读取任意的 Mad Libs 文本,弄清楚要问用户什么,询问用户并读取输入,最后生成完整的 Mad Libs。不过这需要我们学完整个学期的内容,你才能解决。
|
||||||
|
|
||||||
|
请充分测试你的代码,确保其正常工作后,将其作为 `hw1_part1.py` 文件提交到 Submitty,作为作业的第1部分。
|
||||||
|
|
||||||
|
## 第2部分:速度计算 ( 40分 )
|
||||||
|
|
||||||
|
许多运动类 App 会记录用户在步行、跑步、骑自行车或游泳时的时间和距离。有些用户想知道他们每英里的平均配速 ( 以分钟和秒为单位 ),而另一些人想知道他们每小时的平均速度 ( 以英里为单位 )。在很多情况下,我们还想知道特定距离的预计时间。例如,如果我用53分30秒跑了6.3英里,我的平均配速是每英里8分29秒,平均速度是每小时7.07英里,而跑2.7英里的预计时间是22分55秒。
|
||||||
|
|
||||||
|
在本次作业的第2部分,你需要编写一个程序,要求用户输入一次锻炼的时间 ( 分钟和秒 )、跑过的距离 ( 英里 ) 和目标距离 ( 英里 ),并计算平均配速和平均速度。
|
||||||
|
|
||||||
|
文件 `hw1 part2 output.txt` 中提供了程序运行的示例 ( 可以在 `hw01_files.zip` 文件中找到 )。
|
||||||
|
|
||||||
|
你可以假设分钟和秒都是整数,但跑过的距离和目标距离是浮点数。所有的分钟和秒必须保持整数形式,因此请使用整数除法和取模运算。例如:
|
||||||
|
|
||||||
|
```python
|
||||||
|
>>> x = 29.52
|
||||||
|
>>> y = int(x)
|
||||||
|
>>> print(y)
|
||||||
|
29
|
||||||
|
```
|
||||||
|
|
||||||
|
速度的输出应为浮点数,保留2位小数。另外请注意,我们的参考答案在输出计算结果之前会先输出一个空行。
|
||||||
|
|
||||||
|
我们将用示例中的值以及各种不同的值来测试你的代码。请充分测试你的代码,确保其正常工作后,将其作为 `hw1_part2.py` 文件提交到 Submitty,作为作业的第2部分。
|
||||||
|
|
||||||
|
## 第3部分:带框文本框 ( 20分 )
|
||||||
|
|
||||||
|
编写一个程序,要求用户输入一个作为边框的字符,以及文本框的高度和宽度,然后输出一个指定大小的文本框,并用指定的字符作为边框。此外,在文本框内水平垂直居中输出文本框的尺寸。如果无法完美垂直居中,尺寸文本应输出在靠上的位置,即上方留白行数比下方少一行。如果无法完美水平居中,尺寸文本的左侧空格应比右侧少一个。
|
||||||
|
|
||||||
|
你可以假设用户输入的值都是有效的:宽度是一个正整数 ( 7或更大 ),高度是一个正整数 ( 4或更大 ),边框字符是一个单个字符。
|
||||||
|
|
||||||
|
文件 `hw1 part3 output 01.txt` 和 `hw1 part3 output 02.txt` 中提供了程序运行的两个示例 ( 可以在 `hw01_files.zip` 文件中找到 )。
|
||||||
|
|
||||||
|
你需要先将文本框尺寸放入一个字符串中,然后利用字符串的长度来确定尺寸文本所在行的长度。如果你有编程经验,可能会想用循环来生成完整的边框,但 Python 提供的字符串操作功能 ( 第3课内容 ) 使得这没有必要。你不能在程序中使用任何 `if` 语句或循环。我们还没学过它们,而且本题并不需要,使用它们也不会让你的解决方案更优雅。
|
||||||
|
|
||||||
|
我们将用示例中的值以及各种不同的值来测试你的代码。请充分测试你的代码,确保其正常工作后,将其作为 `hw1_part3.py` 文件提交到 Submitty,作为作业的第3部分。
|
||||||
|
|
||||||
|
## 支持文件
|
||||||
|
|
||||||
|
{{< link href="HW1.zip" content="HW1.zip" title="下载 HW1.zip" download="HW1.zip" card=true >}}
|
||||||
|
|
||||||
|
***
|
||||||
|
|
||||||
|
## 参考答案
|
||||||
|
|
||||||
|
### hw1_part1.py
|
||||||
|
|
||||||
|
```python
|
||||||
|
#Prepare variables
|
||||||
|
|
||||||
|
proper_name = ""
|
||||||
|
adjective1 = ""
|
||||||
|
noun1 = ""
|
||||||
|
verb1 = ""
|
||||||
|
verb2 = ""
|
||||||
|
noun2 = ""
|
||||||
|
emotion1 = ""
|
||||||
|
verb3 = ""
|
||||||
|
noun3 = ""
|
||||||
|
season = ""
|
||||||
|
adjective2 = ""
|
||||||
|
emotion2 = ""
|
||||||
|
team_name = ""
|
||||||
|
noun4 = ""
|
||||||
|
adjective3 = ""
|
||||||
|
|
||||||
|
template= """
|
||||||
|
Good morning <proper name>!
|
||||||
|
|
||||||
|
This will be a/an <adjective1> <noun1>! Are you <verb1> forward to it?
|
||||||
|
You will <verb2> a lot of <noun2> and feel <emotion1> when you do.
|
||||||
|
If you do not, you will <verb3> this <noun3>.
|
||||||
|
|
||||||
|
This <season> was <adjective2>. Were you <emotion2> when <team_name> won
|
||||||
|
the <noun4>?
|
||||||
|
|
||||||
|
Have a/an <adjective3> day!"""
|
||||||
|
output = ""
|
||||||
|
|
||||||
|
#Get user's input
|
||||||
|
|
||||||
|
print("Let's play Mad Libs for Homework 1")
|
||||||
|
print("Type one word responses to the following:\n")
|
||||||
|
|
||||||
|
proper_name = input("proper_name ==> ").strip()
|
||||||
|
print(proper_name)
|
||||||
|
adjective1 = input("adjective ==> ").strip()
|
||||||
|
print(adjective1)
|
||||||
|
noun1 = input("noun ==> ").strip()
|
||||||
|
print(noun1)
|
||||||
|
verb1 = input("verb ==> ").strip()
|
||||||
|
print(verb1)
|
||||||
|
verb2 = input("verb ==> ").strip()
|
||||||
|
print(verb2)
|
||||||
|
noun2 = input("noun ==> ").strip()
|
||||||
|
print(noun2)
|
||||||
|
emotion1 = input("emotion ==> ").strip()
|
||||||
|
print(emotion1)
|
||||||
|
verb3 = input("verb ==> ").strip()
|
||||||
|
print(verb3)
|
||||||
|
noun3 = input("noun ==> ").strip()
|
||||||
|
print(noun3)
|
||||||
|
season = input("season ==> ").strip()
|
||||||
|
print(season)
|
||||||
|
adjective2 = input("adjective ==> ").strip()
|
||||||
|
print(adjective2)
|
||||||
|
emotion2 = input("emotion ==> ").strip()
|
||||||
|
print(emotion2)
|
||||||
|
team_name = input("team-name ==> ").strip()
|
||||||
|
print(team_name)
|
||||||
|
noun4 = input("noun ==> ").strip()
|
||||||
|
print(noun4)
|
||||||
|
adjective3 = input("adjective ==> ").strip()
|
||||||
|
print(adjective3)
|
||||||
|
|
||||||
|
#Construct the Mad Lib
|
||||||
|
|
||||||
|
output = template.replace("<proper name>", proper_name)
|
||||||
|
output = output.replace("<adjective1>", adjective1)
|
||||||
|
output = output.replace("<noun1>", noun1)
|
||||||
|
output = output.replace("<verb1>", verb1)
|
||||||
|
output = output.replace("<verb2>", verb2)
|
||||||
|
output = output.replace("<noun2>", noun2)
|
||||||
|
output = output.replace("<emotion1>", emotion1)
|
||||||
|
output = output.replace("<verb3>", verb3)
|
||||||
|
output = output.replace("<noun3>", noun3)
|
||||||
|
output = output.replace("<season>", season)
|
||||||
|
output = output.replace("<adjective2>", adjective2)
|
||||||
|
output = output.replace("<emotion2>", emotion2)
|
||||||
|
output = output.replace("<team_name>", team_name)
|
||||||
|
output = output.replace("<noun4>", noun4)
|
||||||
|
output = output.replace("<adjective3>", adjective3)
|
||||||
|
|
||||||
|
#Print the Mad Lib
|
||||||
|
|
||||||
|
print("\nHere is your Mad Lib...")
|
||||||
|
print(output, end="")
|
||||||
|
```
|
||||||
|
|
||||||
|
### hw1_part2.py
|
||||||
|
|
||||||
|
```python
|
||||||
|
#Perpare Variables
|
||||||
|
|
||||||
|
minutes = 00
|
||||||
|
seconds = 00
|
||||||
|
miles = 00.00
|
||||||
|
target_miles = 00.00
|
||||||
|
|
||||||
|
pace_seconds_per_mile = 00.00
|
||||||
|
pace_seconds = 00
|
||||||
|
pace_minutes = 00
|
||||||
|
|
||||||
|
speed_mph = 00.00
|
||||||
|
|
||||||
|
target_time_total_seconds = 00.00
|
||||||
|
target_time_seconds = 00
|
||||||
|
target_time_minutes = 00
|
||||||
|
|
||||||
|
#Get User Input
|
||||||
|
|
||||||
|
minutes = str(input("Minutes ==> "))
|
||||||
|
print(minutes)
|
||||||
|
seconds = str(input("Seconds ==> "))
|
||||||
|
print(seconds)
|
||||||
|
miles = str(input("Miles ==> "))
|
||||||
|
print(miles)
|
||||||
|
target_miles = str(input("Target Miles ==> "))
|
||||||
|
print(target_miles)
|
||||||
|
|
||||||
|
#Calculate Pace
|
||||||
|
|
||||||
|
pace_seconds_per_mile = (int(minutes) * 60 + int(seconds)) / float(miles)
|
||||||
|
pace_seconds = int(pace_seconds_per_mile % 60)
|
||||||
|
pace_minutes = int(pace_seconds_per_mile // 60)
|
||||||
|
|
||||||
|
#Calculate Speed
|
||||||
|
|
||||||
|
speed_mph = float(miles) / (int(minutes) / 60 + int(seconds) / 3600)
|
||||||
|
|
||||||
|
#Calculate Target Time
|
||||||
|
|
||||||
|
target_time_total_seconds = float(target_miles) * pace_seconds_per_mile
|
||||||
|
target_time_seconds = int(target_time_total_seconds % 60)
|
||||||
|
target_time_minutes = int(target_time_total_seconds // 60)
|
||||||
|
|
||||||
|
#Print Results
|
||||||
|
|
||||||
|
print("\nPace is " + str(pace_minutes) + " minutes and " + str(pace_seconds) + " seconds per mile.")
|
||||||
|
print("Speed is {0:.2f} miles per hour.".format(float(speed_mph)))
|
||||||
|
print("Time to run the target distance of {0:.2f} miles is {1} minutes and {2} seconds.".format(float(target_miles), int(target_time_minutes), int(target_time_seconds)), end="")
|
||||||
|
```
|
||||||
|
|
||||||
|
### hw1_part3.py
|
||||||
|
|
||||||
|
```python
|
||||||
|
#Prepare Variables
|
||||||
|
frame_character = ""
|
||||||
|
height = 0
|
||||||
|
width = 0
|
||||||
|
free_space = 0.0
|
||||||
|
|
||||||
|
#Get user input
|
||||||
|
frame_character = input("Enter frame character ==> ").strip()
|
||||||
|
print(frame_character)
|
||||||
|
height = int(input("Height of box ==> ").strip())
|
||||||
|
print(height)
|
||||||
|
width = int(input("Width of box ==> ").strip())
|
||||||
|
print(width, "\n")
|
||||||
|
|
||||||
|
#Calculate dimensions line
|
||||||
|
dimensions = str(width) + "x" + str(height)
|
||||||
|
free_space = width - 2 - len(dimensions)
|
||||||
|
|
||||||
|
#Calculate the left and right padding considering odd/even width
|
||||||
|
left_space = free_space // 2
|
||||||
|
right_space = free_space // 2 + (free_space % 2)
|
||||||
|
|
||||||
|
#Prepare rows
|
||||||
|
top_bottom_row = frame_character * width
|
||||||
|
empty_row = frame_character + " " * (width - 2) + frame_character
|
||||||
|
dimension_row = frame_character + " " * left_space + dimensions + " " * right_space + frame_character
|
||||||
|
|
||||||
|
#Calculate the number of rows before and after the dimensions row
|
||||||
|
before_rows = ((height - 2) // 2) - ((height - 1) % 2)
|
||||||
|
after_rows = height - 3 - before_rows
|
||||||
|
|
||||||
|
#Output box
|
||||||
|
print("Box:")
|
||||||
|
print(top_bottom_row)
|
||||||
|
print((empty_row + '\n') * before_rows, end="")
|
||||||
|
print(dimension_row)
|
||||||
|
print((empty_row + '\n') * after_rows, end="")
|
||||||
|
print(top_bottom_row)
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user