CS50`s Python Problem Set 0

1.Indoor Voice

题目: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.

解题思路:查阅python官方libraries,熟悉str类型的方法,先使用strip()去除前后多余输入的空格,再使用lower()方法将所有字母转换成小写

我的答案:

def main():
    str=input("Input any text:")
    print(indoor(str))
def indoor(str="hello world"):
    return str.strip().lower()
main() 
2.Playback Speed

题目: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).

解题思路:使用replace()方法将空格替换成"..."

我的答案:

def main():
    playback(input("Input any text:"))
def playback(str="hello world"):
    str=str.strip().replace(" ","...")
    print(str)
main()
3.Making Faces

题目: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 🙂and any :( converted to ☹. All other text should be returned unchanged.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.

解题思路:使用replace()方法,并且注意需要开始使用main()和convert()function

我的答案:

#convert :) to 🙂,:( to 🙁
def main():
    str=input()
    print(convert(str))
def convert(str):
    return str.replace(":)","🙂").replace(":(","🙁")
main()

4.Einstein

题目: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. 

解题思路:使用到int类型的方法,例如int()先将str转换成int类型,pow()可以进行次方运算

我的答案:

def main():
    num=int(input("Input an integer:" ).strip())
    print(calculate(num))
def calculate(num=0):
    c=pow(300000000,2)
    n=num*c
    return n
main()

5.Tip Calculator

题目:太长了懒得复制,大意就是小费计算器,tip=dollars*percent

解题思路:需要结合float 和str的常用方法,对类型进行处理和转换,例如:strip()方法默认去除空格,但是有参数的时候可以移除参数的各种组合,此题中我就移除了输入的空格和“$”符号(其实老师的提示是使用split()进行分隔);round()方法进行四舍五入;

我的答案:

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
    #convert the d from str which split "$"and blank space to float,return a number with two decimals
    dollars=float(d.strip("$ "))
    return round(dollars,2)
def percent_to_float(p):
    # TODO
    """
    convert the p from str which split "%"and blank space to float,
    then make it to the decimals,
    return a number with two decimals
    """
    percent=float(p.strip("% "))/100
    return round(percent,2)
main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值