问题求助
# Q3. Variable name---
## Learning objectives
* Remember the rules of setting variable names in Python and in this course
* Solve a problem using what we have learned from week 2, including:
* Conditional statements
* Loops, including the use of the `break` statement
* Experience that while the tools we have learned so far allow us to solve many different problems, writing code using only those tools can be inefficient and difficult
---
## Background information
A valid variable name in Python has to follow the following rules:
1. It must be composed of letters (`a-zA-Z`), digits (`0-9`) and/or underscore `_` (see Note)
2. It cannot start with a digit
3. It must not be one of the Python keywords
In this course, we further impose the following rules:
4. No mixing case (either all letters used are lowercase (for variables that are supposed to vary), or all letters used are uppercase (for variables that are supposed to be constant))
5. Meaningful names
While it is difficult to check rule 5 ("meaningful" is situational), it is possible to check other rules by a program.
---
## Instructions
Consider a program doing the following:
> Get a variable name provided by a user. Check if the variable name fulfils the rules 1, 2 and 4 above. If that is the case, print `Valid`. If not, print `Invalid`
See the `"Examples"` below for more details. Note that you do not need to check for rule 3 (we will see how to do it in the future).
1. Write one string that satisfies (no need to consider rule 3):
* _Only_ the rule 1: _____
* _Only_ the rule 2: _____
* _Only_ the rule 4: _____
* All but not rule 1: _____
* All but not rule 2: _____
* All but not rule 4: _____
* All 3 rules _and_ composed of letters, digits and underscore: _____
* All 3 rules _and_ composed of underscore only: _____
* All 3 rules _and_ composed of digits and underscore only: _____2. Write your code in (src/variable_name.py) to implement the program specified above
3. Check your program using the strings listed in (1) as inputs (no need to submit)
You are NOT allowed to use any functionality that we have not seen in the course. Essentially, you are not allowed to use indexing or string methods.
This question is relatively difficult.
* You may want to work with your classmates as a group to solve the problem
* Apply decomposition to break down the problem into smaller, more manageable sub-problems
* If you get stuck, feel free to ask your instructors or teachers for help. Alternatively, you can have a look at the hint file available on Moodle
* Even if you cannot fully solve this question, but if your program has some meaningful steps towards solving the original question, you may still get a good grade for this problem set
---
## Assumptions
* Assume the input from the user is not empty
---
## Note
* Note that Python actually accepts some other characters like Γ, but we do not accept them in the course (nor in this question)
---
## Examples
Some examples with valid variable name inputs:
```
>>> Enter a variable name: _a
Valid
```
```
>>> Enter a variable name: _1
Valid
```
```
>>> Enter a variable name: num_students
Valid
```
```
>>> Enter a variable name: numst_udents
Valid
```
Some examples with invalid variable name inputs:
```>>> Enter a variable name: *3
Invalid
```
```
>>> Enter a variable name: 2numst_udents
Invalid
```
```
>>> Enter a variable name: NumStudents
Invalid
```
```
>>> Enter a variable name: Γ
Invalid
```
Note here:
* `Enter a variable name: ` is the input prompt
* `Valid` and `Invalid` are the printouts
* `*3`, `_a`, `num_students`, etc. are inputs from users
求助大神们 这个代码怎么做 谢谢! 这道题要求我们编写一个程序,输入一个变量名,判断它是否符合 Python 变量名的规范。Python 变量名规范包括以下几点:
1. 只能由字母、数字和下划线组成
2. 不能以数字开头
3. 不能使用 Python 中的关键字
4. 变量名中的字母要么全部小写,要么全部大写
5. 变量名要有意义
题目要求我们实现规范 1、2、4,因为要求不使用索引和字符串方法,所以我们可以使用循环和条件语句来判断变量名是否符合规范。
我们可以使用input()函数来获取用户输入的变量名,然后使用for循环遍历变量名的每一个字符,判断每一个字符是否符合规范,如果不符合,就输出Invalid ,并使用break语句退出循环;如果遍历完整个变量名,都没有发现不符合规范的字符,就输出Valid 。
以下是实现代码:
# src/variable_name.py
# 获取用户输入的变量名
variable_name = input("Enter a variable name: ")
# 判断变量名是否符合规范
for i, char in enumerate(variable_name):
# 判断规范 1:只能由字母、数字和下划线组成
if not (char.isalpha() or char.isdigit() or char == "_"):
print("Invalid")
break
# 判断规范 2:不能以数字开头
if i == 0 and char.isdigit():
print("Invalid")
break
# 判断规范 4:变量名中的字母要么全部小写,要么全部大写
if char.isalpha() and char.isupper() != variable_name.isupper():
print("Invalid")
break
else:
# 如果遍历完整个变量名,都没有发现不符合规范的字符,就输出 Valid
print("Valid")
我们可以分别输入题目中要求的 9 种字符串进行测试,看看程序的输出是否符合要求。 问题分析:
这个问题要求我们编写一个程序,从用户获取一个变量名,并检查该变量名是否符合给定的规则。规则包括:
1. 变量名由字母(a-zA-Z)、数字(0-9)和下划线(_)组成;
2. 变量名不能以数字开头;
3. 变量名不能是Python关键字;
4. 变量名要么全部小写(表示可变的变量),要么全部大写(表示常量);
5. 变量名要有意义。
根据这些规则,我们可以编写代码来检查变量名的有效性。
解决方案:
我们可以使用条件语句和循环来实现这个问题。下面是一个可能的解决方案:
# 获取用户输入的变量名
variable_name = input("Enter a variable name: ")
# 检查规则 1、2 和 4
if variable_name.islower() or variable_name.isupper():
# 规则 1、2 和 4 都满足
print("Valid")
else:
# 至少有一个规则不满足
print("Invalid")
在这个解决方案中,我们首先使用input()函数获取用户输入的变量名,并将其存储在variable_name变量中。然后,我们使用条件语句来检查规则 1、2 和 4 是否满足。islower()方法用于检查变量名是否全部小写,isupper()方法用于检查变量名是否全部大写。如果满足这些条件,就打印Valid,否则打印Invalid。
请注意,根据问题描述,我们不需要检查规则 3(Python关键字)是否满足,所以在这个解决方案中我们没有进行相关的检查。
希望这个解决方案对你有帮助!如果你还有任何问题,请随时提问。
球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}
页:
[1]