Define a function that implements the following: ⎻pass in a variable between 0 and 100 called score. ⎻Write an if... else... statement to print a message printing “not in the range,” if the score is not in the range (between 0 and 100). print the grade name indicated in the following table, if the score is between 0 and 100.
时间: 2024-03-16 13:43:27 浏览: 19
Sure, here's a Python function that meets your requirements:
```python
def print_grade(score):
if score < 0 or score > 100:
print("not in the range")
elif score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60:
print("D")
else:
print("F")
```
You can call this function with a score between 0 and 100 as an argument, and it will print the corresponding grade based on the table you provided. If the score is outside that range, it will print "not in the range" instead.
相关问题
Define a function that returns the number of lower cas e, upper case, and digital letters in a string, respectively.
As an AI language model, I can provide you with the code for the requested function in Python:
```python
def count_letters(string):
# Initialize counters
lower_case_count = 0
upper_case_count = 0
digit_count = 0
# Iterate over each character in the string
for char in string:
# Check if the character is lowercase
if char.islower():
lower_case_count += 1
# Check if the character is uppercase
elif char.isupper():
upper_case_count += 1
# Check if the character is a digit
elif char.isdigit():
digit_count += 1
# Return the counts as a tuple
return (lower_case_count, upper_case_count, digit_count)
```
Here's how you can use the function:
```python
string = "ThiS is a StrinG wiTH 1234 DigITS"
counts = count_letters(string)
print(counts) # Output: (17, 8, 4)
```
In this example, the string contains 17 lowercase letters, 8 uppercase letters, and 4 digits.
Define a function first_last(list1) that returns a list with the first and last element of list1 interchanged.
Here's the Python code for the function `first_last(list1)` that returns a list with the first and last element of `list1` interchanged:
```python
def first_last(list1):
if len(list1) < 2:
return list1
else:
first = list1[0]
last = list1[-1]
list1[0] = last
list1[-1] = first
return list1
```
This function takes in a list `list1` as its input and first checks if the length of the list is less than 2. If the length is less than 2, it simply returns the original list. Otherwise, it stores the first element of the list in a variable called `first` and the last element of the list in a variable called `last`. It then interchanges the values of `first` and `last` in the list and returns the modified list.
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)