3.19python作业

本文通过几个简单的Python代码片段展示了如何使用if-else语句进行条件判断。包括了根据外星人的颜色来决定得分、根据年龄判断人生阶段以及根据用户名提供个性化的欢迎消息等例子。
# 5-3
alien_color = 'green'
if alien_color == 'green':
    print('You get 5 points!')

# 5-4
alien_color = 'red'
if 'green' == alien_color:
    print('You get 5 points!')
else:
    print('You get 10 points!')

alien_color = 'green'
if 'green' == alien_color:
    print('You get 5 points!')
else:
    print('You get 10 points!')

# 5-6
age = 10
if age < 2:
    print('He is a baby.')
elif age < 4:
    print('He is learning walking.')
elif age < 13:
    print('He is a child.')
elif age < 20:
    print('He is a teenager.')
elif age < 65:
    print('He is a adult.')
else:
    print('He is an aged man.')

# 5-8
user = 'Mike'
name_list = ['Helen', 'Mike', 'Neil', 'admin', 'Eric']
if user == 'admin':
    print('Hellp admin, would you like to see a status report?')
else:
    print('Hello ' + user + ', thank you for logging in again.')

# 5-8
while name_list:
    name_list.pop()
if len(name_list) == 0:
    print('We need to find some users!')
Last login: Wed Dec 10 22:30:42 on ttys009 mac@bogon ~ % cd /Users/mac/Downloads/Problem_Set_5 mac@bogon Problem_Set_5 % pip install torch torchvision tensorboard zsh: command not found: pip mac@bogon Problem_Set_5 % brew --version Homebrew 5.0.3 mac@bogon Problem_Set_5 % brew install python ==> Auto-updating Homebrew... Adjust how often this is run with `$HOMEBREW_AUTO_UPDATE_SECS` or disable with `$HOMEBREW_NO_AUTO_UPDATE=1`. Hide these hints with `$HOMEBREW_NO_ENV_HINTS=1` (see `man brew`). ==> Fetching downloads for: python@3.14 Warning: Bottle missing, falling back to the default domain... ==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2025- Warning: Bottle missing, falling back to the default domain... ==> Downloading https://ghcr.io/v2/homebrew/core/readline/manifests/8.3.1 ######################################################################### 100.0% Warning: Bottle missing, falling back to the default domain... ==> Downloading https://ghcr.io/v2/homebrew/core/sqlite/manifests/3.50.4-2 ######################################################################### 100.0% ######################################################################### 100.0% Warning: Bottle missing, falling back to the default domain... ==> Downloading https://ghcr.io/v2/homebrew/core/zstd/manifests/1.5.7-1 ✔︎ Bottle mpdecimal (4.0.1) [Downloaded 185.7KB/185.7KB] ✔︎ Bottle xz (5.8.1) [Downloaded 748.1KB/748.1KB] ✔︎ Bottle lz4 (1.10.0) [Downloaded 276.1KB/276.1KB] Warning: Bottle missing, falling back to the default domain...g 98.3KB/133.0KB] ==> Downloading https://ghcr.io/v2/homebrew/core/python/3.14/manifests/3.14.0-1 Warning: Bottle missing, falling back to the default domain... ==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/manifests/3.6.0 ✔︎ Bottle ca-certificates (2025-09-09) [Downloaded 133.0KB/133.0KB] #### 5.9######################################################################### 100.0% ################################ 44.8######################################################################### 100.0% ############################################## 63.6######################################################################### 100.0% ✔︎ Bottle zstd (1.5.7) [Downloaded 788.5KB/788.5KB] ✔︎ Bottle readline (8.3.1) [Downloaded 758.2KB/758.2KB] ✔︎ Bottle sqlite (3.50.4) [Downloaded 2.4MB/ 2.4MB] ✔︎ Bottle openssl@3 (3.6.0) [Downloaded 10.9MB/ 10.9MB] ✔︎ Bottle python@3.14 (3.14.0) [Downloaded 18.7MB/ 18.7MB] ==> Installing dependencies for python@3.14: mpdecimal, ca-certificates, openssl@3, readline, sqlite, xz, lz4 and zstd ==> Installing python@3.14 dependency: mpdecimal ==> Pouring mpdecimal-4.0.1.arm64_sequoia.bottle.tar.gz 🍺 /opt/homebrew/Cellar/mpdecimal/4.0.1: 22 files, 661KB ==> Installing python@3.14 dependency: ca-certificates ==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2025- Already downloaded: /Users/mac/Library/Caches/Homebrew/downloads/4d4fbd31e178e7d46676ee46b8a629a56cf084bda67d2ae2d543a7a9b8d0718a--ca-certificates-2025-09-09-1.bottle_manifest.json ==> Pouring ca-certificates--2025-09-09.all.bottle.1.tar.gz ==> Regenerating CA certificate bundle from keychain, this may take a while... 🍺 /opt/homebrew/Cellar/ca-certificates/2025-09-09: 4 files, 238.4KB ==> Installing python@3.14 dependency: openssl@3 ==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/manifests/3.6.0 Already downloaded: /Users/mac/Library/Caches/Homebrew/downloads/403c903e557d19d801f4c6b4f635c9553af72a487024139a5773e636c884ef9e--openssl@3-3.6.0.bottle_manifest.json ==> Pouring openssl@3--3.6.0.arm64_sequoia.bottle.tar.gz 🍺 /opt/homebrew/Cellar/openssl@3/3.6.0: 7,609 files, 37.7MB ==> Installing python@3.14 dependency: readline ==> Downloading https://ghcr.io/v2/homebrew/core/readline/manifests/8.3.1 Already downloaded: /Users/mac/Library/Caches/Homebrew/downloads/52cb2bb3f0d9e66789968b865501c41ed80dc303eb488939476b309f1d350dc5--readline-8.3.1.bottle_manifest.json ==> Pouring readline--8.3.1.arm64_sequoia.bottle.tar.gz 🍺 /opt/homebrew/Cellar/readline/8.3.1: 56 files, 2.7MB ==> Installing python@3.14 dependency: sqlite ==> Downloading https://ghcr.io/v2/homebrew/core/sqlite/manifests/3.50.4-2 Already downloaded: /Users/mac/Library/Caches/Homebrew/downloads/cc0b46a30077b6cb9f6927fc0c81145398349a0ba416b914b1e5485d0d678231--sqlite-3.50.4-2.bottle_manifest.json ==> Pouring sqlite--3.50.4.arm64_sequoia.bottle.2.tar.gz 🍺 /opt/homebrew/Cellar/sqlite/3.50.4: 13 files, 5.1MB ==> Installing python@3.14 dependency: xz ==> Pouring xz-5.8.1.arm64_sequoia.bottle.tar.gz 🍺 /opt/homebrew/Cellar/xz/5.8.1: 96 files, 2.6MB ==> Installing python@3.14 dependency: lz4 ==> Pouring lz4-1.10.0.arm64_sequoia.bottle.1.tar.gz 🍺 /opt/homebrew/Cellar/lz4/1.10.0: 24 files, 730.6KB ==> Installing python@3.14 dependency: zstd ==> Downloading https://ghcr.io/v2/homebrew/core/zstd/manifests/1.5.7-1 Already downloaded: /Users/mac/Library/Caches/Homebrew/downloads/14f45413747fef87558cd195b1104e55d9739f9574f146051dbd6569ded74a94--zstd-1.5.7-1.bottle_manifest.json ==> Pouring zstd--1.5.7.arm64_sequoia.bottle.1.tar.gz 🍺 /opt/homebrew/Cellar/zstd/1.5.7: 32 files, 2.3MB ==> Installing python@3.14 ==> Pouring python@3.14--3.14.0.arm64_sequoia.bottle.1.tar.gz ==> Caveats Python is installed as /opt/homebrew/bin/python3 Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to `python3`, `python3-config`, `pip3` etc., respectively, are installed into /opt/homebrew/opt/python@3.14/libexec/bin `idle3.14` requires tkinter, which is available separately: brew install python-tk@3.14 See: https://docs.brew.sh/Homebrew-and-Python ==> Summary 🍺 /opt/homebrew/Cellar/python@3.14/3.14.0: 3,743 files, 72.9MB ==> Running `brew cleanup python@3.14`... Disable this behaviour by setting `HOMEBREW_NO_INSTALL_CLEANUP=1`. Hide these hints with `HOMEBREW_NO_ENV_HINTS=1` (see `man brew`). ==> Caveats ==> python@3.14 Python is installed as /opt/homebrew/bin/python3 Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to `python3`, `python3-config`, `pip3` etc., respectively, are installed into /opt/homebrew/opt/python@3.14/libexec/bin `idle3.14` requires tkinter, which is available separately: brew install python-tk@3.14 See: https://docs.brew.sh/Homebrew-and-Python mac@bogon Problem_Set_5 % python3 --version Python 3.13.9 mac@bogon Problem_Set_5 % which python3 /usr/local/bin/python3 mac@bogon Problem_Set_5 % uname -m arm64 mac@bogon Problem_Set_5 % which brew /opt/homebrew/bin/brew mac@bogon Problem_Set_5 % ls /opt/homebrew/bin/python* /opt/homebrew/bin/python3 /opt/homebrew/bin/python3.14 /opt/homebrew/bin/python3-config /opt/homebrew/bin/python3.14-config mac@bogon Problem_Set_5 % echo $PATH /Users/mac/.local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/Library/TeX/texbin:/Library/Frameworks/Mono.framework/Versions/Current/Commands mac@bogon Problem_Set_5 % hash -r mac@bogon Problem_Set_5 % which python3 /opt/homebrew/bin/python3 mac@bogon Problem_Set_5 % cd /Users/mac/Downloads/Problem_Set_5 mac@bogon Problem_Set_5 % ls CV_HW_5.pdf main.py mac@bogon Problem_Set_5 % python -m venv venv zsh: command not found: python mac@bogon Problem_Set_5 % ln -s /opt/homebrew/bin/python3 /opt/homebrew/bin/python mac@bogon Problem_Set_5 % ln -s /opt/homebrew/bin/python3 /opt/homebrew/bin/python ln: /opt/homebrew/bin/python: File exists mac@bogon Problem_Set_5 % python --version Python 3.14.0 mac@bogon Problem_Set_5 % cd /Users/mac/Downloads/Problem_Set_5 python -m venv venv mac@bogon Problem_Set_5 % source venv/bin/activate (venv) mac@bogon Problem_Set_5 % pip install --upgrade pip pip install torch torchvision tensorboard Requirement already satisfied: pip in ./venv/lib/python3.14/site-packages (25.2) Collecting pip Downloading pip-25.3-py3-none-any.whl.metadata (4.7 kB) Downloading pip-25.3-py3-none-any.whl (1.8 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 60.2 kB/s 0:00:31 Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 25.2 Uninstalling pip-25.2: Successfully uninstalled pip-25.2 Successfully installed pip-25.3 Collecting torch Downloading torch-2.9.1-cp314-cp314-macosx_11_0_arm64.whl.metadata (30 kB) Collecting torchvision Downloading torchvision-0.24.1-cp314-cp314-macosx_11_0_arm64.whl.metadata (5.9 kB) Collecting tensorboard Using cached tensorboard-2.20.0-py3-none-any.whl.metadata (1.8 kB) Collecting filelock (from torch) Using cached filelock-3.20.0-py3-none-any.whl.metadata (2.1 kB) Collecting typing-extensions>=4.10.0 (from torch) Using cached typing_extensions-4.15.0-py3-none-any.whl.metadata (3.3 kB) Collecting setuptools (from torch) Using cached setuptools-80.9.0-py3-none-any.whl.metadata (6.6 kB) Collecting sympy>=1.13.3 (from torch) Using cached sympy-1.14.0-py3-none-any.whl.metadata (12 kB) Collecting networkx>=2.5.1 (from torch) Using cached networkx-3.6.1-py3-none-any.whl.metadata (6.8 kB) Collecting jinja2 (from torch) Using cached jinja2-3.1.6-py3-none-any.whl.metadata (2.9 kB) Collecting fsspec>=0.8.5 (from torch) Using cached fsspec-2025.12.0-py3-none-any.whl.metadata (10 kB) Collecting numpy (from torchvision) Downloading numpy-2.3.5-cp314-cp314-macosx_14_0_arm64.whl.metadata (62 kB) Collecting pillow!=8.3.*,>=5.3.0 (from torchvision) Downloading pillow-12.0.0-cp314-cp314-macosx_11_0_arm64.whl.metadata (8.8 kB) Collecting absl-py>=0.4 (from tensorboard) Using cached absl_py-2.3.1-py3-none-any.whl.metadata (3.3 kB) Collecting grpcio>=1.48.2 (from tensorboard) Downloading grpcio-1.76.0-cp314-cp314-macosx_11_0_universal2.whl.metadata (3.7 kB) Collecting markdown>=2.6.8 (from tensorboard) Using cached markdown-3.10-py3-none-any.whl.metadata (5.1 kB) Collecting packaging (from tensorboard) Using cached packaging-25.0-py3-none-any.whl.metadata (3.3 kB) Collecting protobuf!=4.24.0,>=3.19.6 (from tensorboard) Downloading protobuf-6.33.2-cp39-abi3-macosx_10_9_universal2.whl.metadata (593 bytes) Collecting tensorboard-data-server<0.8.0,>=0.7.0 (from tensorboard) Using cached tensorboard_data_server-0.7.2-py3-none-any.whl.metadata (1.1 kB) Collecting werkzeug>=1.0.1 (from tensorboard) Using cached werkzeug-3.1.4-py3-none-any.whl.metadata (4.0 kB) Collecting mpmath<1.4,>=1.1.0 (from sympy>=1.13.3->torch) Using cached mpmath-1.3.0-py3-none-any.whl.metadata (8.6 kB) Collecting markupsafe>=2.1.1 (from werkzeug>=1.0.1->tensorboard) Downloading markupsafe-3.0.3-cp314-cp314-macosx_11_0_arm64.whl.metadata (2.7 kB) Downloading torch-2.9.1-cp314-cp314-macosx_11_0_arm64.whl (74.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 74.4/74.4 MB 85.7 kB/s 0:22:07 Downloading torchvision-0.24.1-cp314-cp314-macosx_11_0_arm64.whl (2.0 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 83.2 kB/s 0:00:24 Using cached tensorboard-2.20.0-py3-none-any.whl (5.5 MB) Using cached tensorboard_data_server-0.7.2-py3-none-any.whl (2.4 kB) Using cached absl_py-2.3.1-py3-none-any.whl (135 kB) Using cached fsspec-2025.12.0-py3-none-any.whl (201 kB) Downloading grpcio-1.76.0-cp314-cp314-macosx_11_0_universal2.whl (11.8 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.8/11.8 MB 77.5 kB/s 0:02:32 Using cached typing_extensions-4.15.0-py3-none-any.whl (44 kB) Using cached markdown-3.10-py3-none-any.whl (107 kB) Using cached networkx-3.6.1-py3-none-any.whl (2.1 MB) Downloading numpy-2.3.5-cp314-cp314-macosx_14_0_arm64.whl (5.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.1/5.1 MB 70.1 kB/s 0:01:07 Downloading pillow-12.0.0-cp314-cp314-macosx_11_0_arm64.whl (4.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.7/4.7 MB 82.5 kB/s 0:00:58 Downloading protobuf-6.33.2-cp39-abi3-macosx_10_9_universal2.whl (427 kB) Using cached setuptools-80.9.0-py3-none-any.whl (1.2 MB) Using cached sympy-1.14.0-py3-none-any.whl (6.3 MB) Using cached mpmath-1.3.0-py3-none-any.whl (536 kB) Using cached werkzeug-3.1.4-py3-none-any.whl (224 kB) Downloading markupsafe-3.0.3-cp314-cp314-macosx_11_0_arm64.whl (12 kB) Using cached filelock-3.20.0-py3-none-any.whl (16 kB) Using cached jinja2-3.1.6-py3-none-any.whl (134 kB) Using cached packaging-25.0-py3-none-any.whl (66 kB) Installing collected packages: mpmath, typing-extensions, tensorboard-data-server, sympy, setuptools, protobuf, pillow, packaging, numpy, networkx, markupsafe, markdown, fsspec, filelock, absl-py, werkzeug, jinja2, grpcio, torch, tensorboard, torchvision Successfully installed absl-py-2.3.1 filelock-3.20.0 fsspec-2025.12.0 grpcio-1.76.0 jinja2-3.1.6 markdown-3.10 markupsafe-3.0.3 mpmath-1.3.0 networkx-3.6.1 numpy-2.3.5 packaging-25.0 pillow-12.0.0 protobuf-6.33.2 setuptools-80.9.0 sympy-1.14.0 tensorboard-2.20.0 tensorboard-data-server-0.7.2 torch-2.9.1 torchvision-0.24.1 typing-extensions-4.15.0 werkzeug-3.1.4 (venv) mac@bogon Problem_Set_5 %
最新发布
12-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值