|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# 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](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
求助大神们 这个代码怎么做 谢谢!
问题分析:
这个问题要求我们编写一个程序,从用户获取一个变量名,并检查该变量名是否符合给定的规则。规则包括:
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关键字)是否满足,所以在这个解决方案中我们没有进行相关的检查。
希望这个解决方案对你有帮助!如果你还有任何问题,请随时提问。
球一个最佳答案谢谢啦!这对我非常重要!  
|
|