CS50python 作业LECUTR2

In some languages, it’s common to use camel case (otherwise known as “mixed case”) for variables’ names when those names comprise multiple words, whereby the first letter of the first word is lowercase but the first letter of each subsequent word is uppercase. For instance, whereas a variable for a user’s name might be called name, a variable for a user’s first name might be called firstName, and a variable for a user’s preferred first name (e.g., nickname) might be called preferredFirstName.

Python, by contrast, recommends snake case, whereby words are instead separated by underscores (_), with all letters in lowercase. For instance, those same variables would be called namefirst_name, and preferred_first_name, respectively, in Python.

In a file called camel.py, implement a program that prompts the user for the name of a variable in camel case and outputs the corresponding name in snake case. Assume that the user’s input will indeed be in camel case.

def main():
    camelcase = input("camelcase:")
    print("snake_case:",snake_case(camelcase))

def snake_case(words):
    name = ""
    for s in words:
        if s.isupper():
            name = name + "_" + s.lower()
        else:
            name = name + s
    return name

main()

Suppose that a machine sells bottles of Coca-Cola (Coke) for 50 cents and only accepts coins in these denominations: 25 cents, 10 cents, and 5 cents.

In a file called coke.py, implement a program that prompts the user to insert a coin, one at a time, each time informing the user of the amount due. Once the user has inputted at least 50 cents, output how many cents in change the user is owed. Assume that the user will only input integers, and ignore any integer that isn’t an accepted denomination.

print("amount_due:50")
amount = int(50)
insert = 0
change = 0
coin = 0

while 50 > coin:
    insert= int(input("Insert Coin:"))

    if insert in [25,20,5]:
        coin +=insert
        amount = amount - insert
        print("amount_due:",amount)

        if amount <= 0:
            print("change_owed:",amount)
            break

    else:
        print("amount_due:",amount)

When texting or tweeting, it’s not uncommon to shorten words to save time or space, as by omitting vowels, much like Twitter was originally called twttr. In a file called twttr.py, implement a program that prompts the user for a str of text and then outputs that same text but with all vowels (A, E, I, O, and U) omitted, whether inputted in uppercase or lowercase.

def main():
    get = input("Input:")
    print("Output:",twttr(get))

def twttr(inpu):
    con = ""
    inpu = inpu.lower()
    for i in inpu:
        if i in ["a","e","i","o","u"]:
            pass
        else:
            con += i
    return con

main()

In Massachusetts, home to Harvard University, it’s possible to request a vanity license plate for your car, with your choice of letters and numbers instead of random ones. Among the requirements, though, are:

  • “All vanity plates must start with at least two letters.”
  • “… vanity plates may contain a maximum of 6 characters (letters or numbers) and a minimum of 2 characters.”
  • “Numbers cannot be used in the middle of a plate; they must come at the end. For example, AAA222 would be an acceptable … vanity plate; AAA22A would not be acceptable. The first number used cannot be a ‘0’.”
  • “No periods, spaces, or punctuation marks are allowed.”

In plates.py, implement a program that prompts the user for a vanity plate and then output Valid if meets all of the requirements or Invalid if it does not. Assume that any letters in the user’s input will be uppercase. Structure your program per the below, wherein is_valid returns True if s meets all requirements and False if it does not. Assume that s will be a str. You’re welcome to implement additional functions for is_valid to call (e.g., one function per requirement).

def main():
    plate = input("Plate: ")
    if is_valid(plate):
        print("Valid")
    else:
        print("Invalid")


def is_valid(s):
    
    # plates contain a maximum of 6 characters and a minimum of 2 characters.
    if len(s) < 2 or len(s) > 6:
        return False

    # plates must start with at least two letters
    if s[0].isdigit():
        return False
    if s[1].isdigit():
        return False

    # plates end with numbers
    if s[len(s)-1].isalpha():
          return False

    # The first number used cannot be a ‘0’
    if s[2] == "0":
         return False

    return True

main()

The U.S. Food & Drug Adminstration (FDA) offers downloadable/printable posters that “show nutrition information for the 20 most frequently consumed raw fruits … in the United States. Retail stores are welcome to download the posters, print, display and/or distribute them to consumers in close proximity to the relevant foods in the stores.”

In a file called nutrition.py, implement a program that prompts consumers users to input a fruit (case-insensitively) and then outputs the number of calories in one portion of that fruit, per the FDA’s poster for fruits, which is also available as text. Capitalization aside, assume that users will input fruits exactly as written in the poster (e.g., strawberries, not strawberry). Ignore any input that isn’t a fruit.

fruits = {
    "apple":130,
    "avocado":50,
    "banana":110,
    "lemon":15,
    "pear":100,
    "watermelon":80,
    "lime":20,
    "grapes":90,
    "orange":80,
    "peach":60,
}

fruit = input("Item:")
if fruit in fruits:
    print("Calories:",fruits[fruit])
else:
    pass

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值