Python 程序接受三个数字并打印出所有可能的数字组合
当需要从用户那里输入数字时打印出所有可能的数字组合时,可以使用嵌套循环。
下面是对此进行演示的示例 -
示例
first_num = int(input("Enter the first number..."))
second_num = int(input("Enter the second number..."))
third_num = int(input("Enter the third number..."))
my_list = []
print("The first number is ")
print(first_num)
print("The second number is ")
print(second_num)
print("The third number is ")
print(third_num)
my_list.append(first_num)
my_list.append(second_num)
my_list.append(third_num)
for i in range(0,3):
for j in range(0,3):
for k in range(0,3):
if(i!=j&j!=k&k!=i):
print(my_list[i],my_list[j],my_list[k])输出
Enter the first number...3 Enter the second number...5 Enter the third number...8 The first number is 3 The second number is 5 The third number is 8 3 5 8 3 8 5 5 3 8 5 8 3 8 3 5 8 5 3
说明
从用户那里输入三个数字。
创建一个空列表。
在控制台上显示这三个数字。
将这些数字附加到空列表中。
使用三个嵌套循环,并对这些数字进行迭代。
如果它们不相等,则将它们的组合显示为控制台上的输出。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP