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