人工智能与python学习速查

本文介绍了如何在Kaggle学习中使用Python编写函数,包括将数字成绩转换为字母等级(get_grade函数)和根据戒指材质及刻字数量计算项目成本(cost_of_project函数),涉及条件语句和字符串长度操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

kaggle学习教程

备注

尽量看英语,不认识的就大概猜一下什么意思。

遗忘速查:(一些经常记不住的东西)

# string
len(str) # string的长度

基础

Conditions and Conditional Statements

准备工作:

from learntools.core import binder
binder.bind(globals())
from learntools.intro_to_programming.ex4 import *
print('Setup complete.')

Question 1 if else

You work at a college admissions office. When inspecting a dataset of college applicants, you notice that some students have represented their grades with letters ("A""B""C""D""F"), whereas others have represented their grades with a number between 0 and 100.

You realize that for consistency, all of the grades should be formatted in the same way, and you decide to format them all as letters. For the conversion, you decide to assign:

  • "A" - any grade 90-100, inclusive
  • "B" - any grade 80-89, inclusive
  • "C" - any grade 70-79, inclusive
  • "D" - any grade 60-69, inclusive
  • "F" - any grade <60

Write a function get_grade() that takes as input:

  • score - an integer 0-100 corresponding to a numerical grade

It should return a Python string with the letter grade that it corresponds to. For instance,

  • A score of 85 corresponds to a B grade. In other words, get_grade(85) should return "B".
  • A score of 49 corresponds to an F grade. In other words, get_grade(49) should return "F".

Make sure that when supplying the grade that is returned by the function, it is enclosed in quotes. (For instance, if you want to return "A", you should write return "A" and not return A.)

def get_grade(score):
    if 90<=score:  grade = "A" 
    elif 80<=score: grade = "B" 
    elif 70<=score:  grade = "C" 
    elif 60<=score:  grade = "D" 
    else: grade = "F" 
    return grade

 这个问题比较简单,定义函数,简单的逻辑判断就可以完成。

Question 2

In the exercise for the previous lesson, you wrote a function cost_of_project() that estimated the price of rings for an online shop that sells rings with custom engravings. This function did not use conditional statements. In this exercise, you will rewrite the function to use conditional statements. Recall that the online shop has the following price structure:

  • Gold plated rings have a base cost of $50, and you charge $7 per engraved unit.
  • Solid gold rings have a base cost of $100, and you charge $10 per engraved unit.
  • Spaces and punctuation are counted as engraved units.

Your function cost_of_project() takes two arguments:

  • engraving - a Python string with the text of the engraving
  • solid_gold - a Boolean that indicates whether the ring is solid gold

It should return the cost of the project.

The function has been partially completed for you, and you need to fill in the blanks to complete the function.

def cost_of_project(engraving, solid_gold):
    if solid_gold == True:
        cost = 100 + len(engraving) * 10
    else:
        cost = 50 + len(engraving) * 7
    return cost

 python中string长度函数,len(str)。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值