设为首页 加入收藏

TOP

CS50-Python实验3,4(一)
2023-07-23 13:43:05 】 浏览:29
Tags:CS50-Python 实验

Week3 Exceptions

Fuel Gauge

题目描述:

输入分数字符串,判断并输出相应的百分数;特例不足1%输出E,超出99%输出F

思路:

1,从字符串中取出x,y;

2,按题中要求计算输出;

题解:

while True:
    try:
        ## 取出x,y
        x, z, y= input("Fraction: ")
        x, y = int(x), int (y)
        
        ##防止出错
        if y == 0:
            raise ZeroDivisionError
        if x > y:
            raise ValueError
        break
    except(ValueError, ZeroDivisionError):
        pass

## 模拟题
if 0 < x/y < 0.99:
    print(f"{round(x/y*100)}%")
elif x/y >= 0.99:
    print("F")
else:
    print("E")

Felipe’s Taqueria

题目描述:

根据用户输入的信息,匹配菜单价格,最终算出最终价格。

题解:

## 菜单信息
menu = {
    "Baja Taco": 4.00,
    "Burrito": 7.50,
    "Bowl": 8.50,
    "Nachos": 11.00,
    "Quesadilla": 8.50,
    "Super Burrito": 8.50,
    "Super Quesadilla": 9.50,
    "Taco": 3.00,
    "Tortilla Salad": 8.00,
}

##total:结果
total = 0

## 计算输出
while True:
    try:
        ## title(): 首字母大写其余小写
        item = input("Item: ").title() 
        if item in menu:
            total += menu[item]
            print(f"Total: ${total:.2f}")
    except EOFError:
        print()
        break

Grocery List

题目描述:

记录用户输入商品,最终输出已输入的商品以及商品数量;

题解:

## 存储商品信息
list = {}


while True:
    ## 输入处理
    try:
        ## upper():字符串大写
        item = input("").upper()
    except EOFError:
        print()
        break
	
    ## 记录商品
    if item in list:
        list[item] += 1
    else:
        list[item] = 1

## 输出结果
for item, cnt in sorted(list.items()):
    print(f"{cnt} {item}")

Outdated

题目描述:

用户输入9/8/1636,September 8, 1636形式,系统转换输出1636-09-08;若用户输出其他形式抛异常。

题解:

## 月份记录
months = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]

##思路:分情况讨论
while True:
    date = input("Date: ")
    month,day,year = "", "", ""
    try:
        ## 9/8/1636
        if len(date.split("/")) == 3: 
            date = date.split("/")
            month, day, year = map(int, date)
        ## September 8, 1636
        else:
            date = date.split(",")
            if len(date) == 2:
                month, day = date[0].split()
                month = months.index(month)+1
                day, year = int(day), int(date[1])
        ##输出
        if 1<=int(month)<=12 and 1<=int(day)<=31:
            print(f"{year:04}-{month:02}-{day:02}")
            break
    except ValueError:
        pass

Week4 Libraries

Emojize

题目描述:

练习python调用库操作;

题解:

from emoji import emojize

print("Output:", emojize(input("Input: ")))

Frank, Ian and Glen’s Letters

题目描述:

The documentation for pyfiglet isn’t very clear, but you can use the module as follows:

from pyfiglet import Figlet

figlet = Figlet()

You can then get a list of available fonts with code like this:

figlet.getFonts()

You can set the font with code like this, wherein f is the font’s name as a str:

figlet.setFont(font=f)

And you can output text in that font with code like this, wherein s is that text as a str:

print(figlet.renderText(s))

题解:

from pyfiglet import Figlet
import random
import sys


fonts = Figlet().getFonts()

# Zero if the user would like to output text in a random font.
# Two if the user would like to output text in a specific font, in which case the first of the two should be -f or --font, and the second of the two should be the name of the font.
if len(sys.argv) == 1:
    font = random.choice(fonts)
elif len(sys.argv) == 3:
    if sys.
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇获取Python函数信息的方法 下一篇Django笔记十六之aggregate聚合操..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目