Article directory
I. Introduction
Hello, I am Xiaoyu Youth, an independent programmer.
In the previous article, we briefly introduced the basic functions of InsCode, please read this article No more empty talk, use InsCode to show your programming skills .
And now it has been a while, InsCode has brought a new evolution, InsCode AI Chat allows you to help you optimize your code through chat.
2. Use AI to assist in code completion
Next, let's start from practice, based on InsCode's AI-assisted programming, and write a calculator implemented in Python.
1. Create a project based on a template
Here we use the platform for programming throughout the process. Starting from creating a project, the method of creating a project based on an existing template can help us quickly build a basic environment.
After filling out some basic information on the form, we click Create Project.
After we wait for the project to be created, we will automatically enter the main interface of the project. In the case of not moving any code, click the run button directly, and the project will be run directly on the right side of the page. If your project is a web application, the webpage preview window will open.
2. Use AI to help develop ideas
We can press the shortcut keys Ctrl + L in the editor to call out InsCode AI Chat , where you can interact with Ai in text.
For example, in this case, we need to enumerate several sorting algorithms, so I don’t want to think about it myself, I want to hand it over to Ai, so I can ask like this.
我要用python实现一个交互式的能运行加减乘除的计算器,你有实现思路吗?
As shown in the figure below, we can get the answer from Ai.
Ai gave an implementation idea, probably because the theme is relatively simple, it even directly output the complete project code at the end.
The idea of this implementation is not the same as I expected, so I will give him some hints, for example, I want the user to input numbers, operators, and numbers in sequence, and then automatically output the results, as shown in the figure below.
Tip: In actual projects, we can use Ai to continuously optimize the solution to the problem, and get the answer we want by enriching the details of the requirements.
3. Use AI-assisted code generation
In InsCode, we can ask Ai to help produce part of the code by writing comments.
The specific operation is to first write a comment in the code editor, then select the comment and right-click the menu to complete the code.
实现一个交互式的能运行加减乘除的计算器,用户依次输入数字、运算符、数字,之后自动输出结果
This operation realizes sending comments and "continue to complete the above code" to InsCode AI Chat, and then AI will output codes and detailed explanations according to the current project environment.
This is equivalent to being a programming teacher who answers all questions. The more accurate your questions are, the more accurate your answers will be.
The full code we got is below.
# 实现一个交互式的能运行加减乘除的计算器,用户依次输入数字、运算符、数字,之后自动输出结果
while True:
num1 = float(input("请输入第一个数字:"))
operator = input("请输入运算符:(+,-,*,/或exit退出)")
num2 = float(input("请输入第二个数字:"))
if operator == "+":
result = num1 + num2
print("计算结果:", result)
elif operator == "-":
result = num1 - num2
print("计算结果:", result)
elif operator == "*":
result = num1 * num2
print("计算结果:", result)
elif operator == "/":
if num2 == 0:
print("除数不能为0,请重新输入")
else:
result = num1 / num2
print("计算结果:", result)
elif operator == "exit":
break
else:
print("无效的运算符,请重新输入")
4. Use AI to help optimize code
Judging from the current development trend, our actual project code will definitely not be completely generated by Ai.
You can also use InsCode AI Chat as a pair programming colleague to help you optimize your code.
Still taking the above code as an example, we artificially remove the judgment that the divisor cannot be zero. Then select this part of the code, right click and select optimize code .
We can see that AI solves the divisor by zero problem in another way.
In this code, we define a function called calculator, which contains a while loop to realize the continuous operation of the program, and uses try-except to handle possible errors, such as entering an invalid number or dividing by zero.
In the function body, we first input two numbers and an operator, then use the if-elif-else statement to judge the type of the operator, perform corresponding calculations and output the result. When an invalid operator is entered, we prompt the user to re-enter it. In addition, when an invalid number is entered or the divisor is zero, there will be a corresponding error message.
Finally, the calculator function is called in the main.py file to execute the calculator program.
Tip: At present, the style of AI generated code is not uniform. If you want to use it in actual projects, you need to pay attention to the code style.
The complete code generated this time is as follows.
def calculator():
while True:
try:
num1 = float(input("请输入第一个数字:"))
operator = input("请输入运算符:(+,-,*,/或exit退出)")
if operator == "exit":
break
num2 = float(input("请输入第二个数字:"))
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
result = num1 / num2
else:
print("无效的运算符,请重新输入")
continue
print("计算结果:", result)
except ValueError:
print("无效的数字,请重新输入")
except ZeroDivisionError:
print("除数不能为0,请重新输入")
if __name__ == '__main__':
calculator()
3. Suggestions for using InsCode AI Chat
According to my experience, I suggest you use InsCode AI Chat like this.
- Split the problem as much as possible, precise and clear problems can improve the quality of the output code;
- Use the code optimization function repeatedly. The code given by AI cannot be guaranteed to be optimal for the first time. It is recommended to continuously optimize the code with problems to improve the code quality;
- Use the auxiliary function of the right button, which allows AI to analyze code and requirements based on the programming language used by the current project
- Don't trust the AI code completely, try to use it as a reference
Four. Summary
The addition of the InsCode AI Chat function makes it easier for school students to learn programming.
As long as your problem is clear enough, you will get relatively satisfactory results. Of course, what the problem description requires is our in-depth thinking.
When the how becomes easy to grasp, the why becomes more and more precious.