program count_positive_negative;var i, n, positive_count, negative_count: integer;begin positive_count := 0; negative_count := 0; for i := 1 to 20 do begin write('请输入第', i, '个数:'); readln(n); if n > 0 then positive_count := positive_count + 1 else if n < 0 then negative_count := negative_count + 1; end; writeln('正数个数:', positive_count); writeln('负数个数:', negative_count); readln;end.
时间: 2023-05-28 21:06:55 浏览: 48
As an AI language model, I can understand the code you have shared. It is a simple Pascal program that counts the number of positive and negative integers entered by the user. The program uses a for loop to prompt the user to input 20 integers and then check if the integer is positive or negative using if-else statements. The positive and negative counts are stored in the variables positive_count and negative_count respectively. Finally, the program displays the counts of positive and negative integers.
相关问题
python if __name__ == __main__:用法
`if __name__ == "__main__":` 是 Python 中常用的一个条件语句,它通常用于判断当前模块是否是运行在主程序中,或者是被其他模块 import 而导入运行的。
具体来说,当一个 Python 文件直接被执行时,`__name__` 变量的值会被自动设置为 `"__main__"`,如果被导入时,`__name__` 的值就是模块名(即文件名)。
因此,使用 `if __name__ == "__main__":` 可以方便地将一些只在作为主程序运行时才执行的代码放在这个条件语句中,而避免在模块被导入时执行这些代码。
下面是一个简单的示例,假设我们有一个名为 `example.py` 的 Python 模块,里面包含一些函数和变量:
```
# example.py
def add(x, y):
return x + y
def sub(x, y):
return x - y
pi = 3.1415926
```
如果我们在命令行中直接执行 `example.py`,那么 `__name__` 的值就是 `"__main__"`,我们可以在条件语句中添加一些测试代码:
```
# example.py
def add(x, y):
return x + y
def sub(x, y):
return x - y
pi = 3.1415926
if __name__ == "__main__":
print("This is the main program.")
print("The result of 1 + 2 is", add(1, 2))
print("The value of pi is", pi)
```
如果我们将 `example.py` 导入到另一个模块中,那么 `__name__` 的值就是模块名(即文件名),条件语句中的代码就不会被执行:
```
# test.py
import example
print("This is a test program.")
print("The result of 3 - 4 is", example.sub(3, 4))
```
这样,当我们执行 `test.py` 时,`example.py` 中的测试代码就不会被执行,只有 `test.py` 的代码会被执行。
if "__main__" == __name__:
If "__main__" == __name__:, it is a conditional statement commonly used in Python scripts. It checks if the current module is being run directly as the main program. In other words, it ensures that certain code is only executed when the module is run directly and not when it is imported as a module in another script.
This check is useful when you have some code that should only run when the script is executed directly, such as initializing variables, defining functions, or running specific operations. When the module is imported, this code block will be skipped.
Here's an example to illustrate its usage:
```python
def main():
# code to be executed when the script is run directly
pass
if "__main__" == __name__:
main()
```
In this example, the `main()` function will only be executed if the script is run directly. If the module is imported by another script, the `main()` function will not be executed.