CS50P Problem Set 0

课程:CS50’s Introduction to Programming with Python

CS50 Python 2025 课程的问题集作业提交。主要提供一个本人的解题思路,仅供参考。

Indoor Voice

Problem:

WRITING IN ALL CAPS IS LIKE YELLING.
全部大写写作就像大喊大叫。

Best to use your “indoor voice” sometimes, writing entirely in lowercase.
最好有时使用你的“室内声音”,完全用小写写。

In a file called indoor.py, implement a program in Python that prompts the user for input and then outputs that same input in lowercase. Punctuation and whitespace should be outputted unchanged. You’re welcome, but not required, to prompt the user explicitly, as by passing a str of your own as an argument to input.
在名为 indoor.py 的文件中,用 Python 实现一个程序,提示用户输入,然后以小写输出相同的输入。标点符号和空格应原封不动地输出。欢迎但不是必需的,明确提示用户,例如将您自己的 str 作为输入的参数传递。

Submit:

indoor = input()
print(indoor.lower())

Playback Speed

Problem:

Some people have a habit of lecturing speaking rather quickly, and it’d be nice to slow them down, a la YouTube’s 0.75 playback speed, or even by having them pause between words.
有些人有讲课速度相当快的习惯,最好放慢他们的速度,就像 YouTube 的 0.75 播放速度一样,甚至让他们在单词之间暂停。

In a file called playback.py, implement a program in Python that prompts the user for input and then outputs that same input, replacing each space with ... (i.e., three periods).
在名为 playback.py 的文件中,用 Python 实现一个程序,该程序提示用户输入,然后输出相同的输入,将每个空格替换为 ...(即三个句点)。

Submit:

playback = input()
print(playback.replace(" ", "..."))

Making Faces

Problem:

Before there were emoji, there were emoticons, whereby text like :) was a happy face and text like :( was a sad face. Nowadays, programs tend to convert emoticons to emoji automatically!
在表情符号出现之前,有表情符号 ,像 :) 这样的文字是一张快乐的脸,像 :( 这样的文字是一张悲伤的脸。如今,程序倾向于自动将表情符号转换为表情符号!

In a file called faces.py, implement a function called convert that accepts a str as input and returns that same input with any :) converted to 🙂 (otherwise known as a slightly smiling face) and any :( converted to 🙁 (otherwise known as a slightly frowning face). All other text should be returned unchanged.
在名为 faces.py 的文件中,实现一个名为 convert 的函数,该函数接受 str 作为输入,并返回相同的输入,其中包含转换到 🙂 的任何 :)(也称为微微微笑的脸 )和转换为的任何 🙁 :((也称为微微皱眉的脸 )。所有其他文本应原封不动地返回。

Then, in that same file, implement a function called main that prompts the user for input, calls convert on that input, and prints the result. You’re welcome, but not required, to prompt the user explicitly, as by passing a str of your own as an argument to input. Be sure to call main at the bottom of your file.
然后,在同一文件中,实现一个名为 main 的函数,该函数提示用户输入,对该输入调用 convert,并打印结果。欢迎但不是必需的,明确提示用户,例如将您自己的 str 作为输入的参数传递。请务必在文件底部调用 main

Submit:

def convert(text):
    text = text.replace(":)", "🙂")
    text = text.replace(":(", "🙁")
    return text

def main():
    faces = input()
    print(convert(faces))

main()

Einstein

Problem:

Even if you haven’t studied physics (recently or ever!), you might have heard that 𝐸 =𝑚⁢𝑐2, wherein 𝐸 represents energy (measured in Joules), 𝑚 represents mass (measured in kilograms), and 𝑐 represents the speed of light (measured approximately as 300000000 meters per second), per Albert Einstein et al. Essentially, the formula means that mass and energy are equivalent.
即使您没有学习过物理学(最近或曾经!),您也可能听说过, 𝐸 =𝑚⁢𝑐2 其中 𝐸 代表能量(以焦耳为单位), 𝑚 代表质量(以千克为单位),代表 𝑐 光速(测量约为每秒 300000000 米),根据阿尔伯特·爱因斯坦等人的说法。从本质上讲,该公式意味着质量和能量是等价的。

In a file called einstein.py, implement a program in Python that prompts the user for mass as an integer (in kilograms) and then outputs the equivalent number of Joules as an integer. Assume that the user will input an integer.
在名为 einstein.py 的文件中,用 Python 实现一个程序,该程序提示用户以整数(以千克为单位)输入质量,然后输出等效的焦耳数作为整数。假设用户将输入一个整数。

Submit:

def energy(m):
    E = m * 300000000 ** 2
    return E

def main():
    einstein = int(input("m: "))
    print(energy(einstein))

main()

Tip Calculator

And now for my Wizard tip calculator.现在是我的向导小费计算器。

— Morty Seinfeld  — 莫蒂·宋飞

In the United States, it’s customary to leave a tip for your server after dining in a restaurant, typically an amount equal to 15% or more of your meal’s cost. Not to worry, though, we’ve written a tip calculator for you, below!
在美国,习惯上在餐厅用餐后给服务员留下小费,金额通常相当于用餐费用的 15% 或更多。不过不用担心,我们在下面为您编写了一个小费计算器!

def main():
    dollars = dollars_to_float(input("How much was the meal? "))
    percent = percent_to_float(input("What percentage would you like to tip? "))
    tip = dollars * percent
    print(f"Leave ${tip:.2f}")


def dollars_to_float(d):
    # TODO


def percent_to_float(p):
    # TODO


main()

Well, we’ve written most of a tip calculator for you. Unfortunately, we didn’t have time to implement two functions:
好吧,我们已经为您编写了大部分小费计算器。不幸的是,我们没有时间实现两个函数:

  • dollars_to_float, which should accept a str as input (formatted as $##.##, wherein each # is a decimal digit), remove the leading $, and return the amount as a float. For instance, given $50.00 as input, it should return 50.0.
    dollars_to_float,它应该接受一个 str 作为输入(格式为 $##.##,其中每个 # 是十进制数字),删除前导 $,并将金额作为浮点数返回。例如,给定 $50.00 作为输入,它应该返回 50.0
  • percent_to_float, which should accept a str as input (formatted as ##%, wherein each # is a decimal digit), remove the trailing %, and return the percentage as a float. For instance, given 15% as input, it should return 0.15.
    percent_to_float,它应该接受 str 作为输入(格式为 ##%, 其中每个 # 是十进制数字),删除尾随 %, 并将百分比作为浮点数返回。例如,给定 15% 作为输入,它应该返回 0.15

Assume that the user will input values in the expected formats.
假设用户将以预期格式输入值。

Submit:

def main():
    dollars = dollars_to_float(input("How much was the meal? "))
    percent = percent_to_float(input("What percentage would you like to tip? "))
    tip = dollars * percent
    print(f"Leave ${tip:.2f}")


def dollars_to_float(d):
    # TODO
    d = float(d.replace("$", ""))
    return d


def percent_to_float(p):
    # TODO
    p = float(p.replace("%", ""))
    return p / 100


main()

关于提交

两年之后又开始学习 CS50 相关课程,本次主要遇到一个代码提交的问题,之前在 CS50 X 课程上未曾遇到,这次也是头疼了很久,主要问题就是使用 Check50 或者 Submit50 时候遇到下面这样的提示:

You might be using your GitHub password to log in, but that's no longer possible.

这表示 GitHub 已经不允许使用账号密码登录了(这是安全策略更新后的结果)。
现在 check50(和 submit50)需要用 Personal Access Token(个人访问令牌) 来登录验证。

解决步骤:

生成 GitHub Token

  1. 登录 GitHub
  2. 点击右上角头像 → Settings
  3. 左边菜单中选择 Developer settings
  4. 然后选择 Personal access tokens → Tokens (classic)
  5. 点击 Generate new token (classic)
  6. 选择有效期(例如 90 天)
  7. 勾选这些权限:
    • repo
    • read:org
    • user:email
  8. 点击最下方的 Generate token

GitHub 会生成一串长长的字符,比如:

ghp_abCdEfG1234567890xyzXYZ

将这串字符串复制进 Verifying…… 时候出现的 Password 提示中即可。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇