需求:假设让用户输入两次数字,第一次为12,第二次为34,实现的组合为13,14,23,24;
即第一次输入的每一个数字与第2次输入的每一个数字进行俩俩组合。
1
2
3
4
5
6
7
8
9
10
11
|
num1 = input ( '请输入0-9之间任意一个或多个数字:' )
num2 = input ( '请再次输入0-9之间任意一个或多个数字:' )
def account(num1,num2):
print ( "两次输入数字的组合即将开始" )
list1 = [(x,y) for x in num1 for y in num2 ]
num3 = []
for list2 in list1:
print (''.join([ str (x) for x in list2]))
num3.append(''.join([ str (x) for x in list2]))
print ( "组合完成,共有" + str ( len (num3)) + "对组合!" )
account(num1,num2) |
方法二,利用tkinter实现可视化按钮操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
from tkinter import *
root = Tk()
root.title( "数字组合工具" )
Label(root,text = "数字1" ).grid(row = 0 ,column = 0 )
Label(root,text = "数字2" ).grid(row = 1 ,column = 0 )
e1 = Entry(root)
e2 = Entry(root)
e1.grid(row = 0 ,column = 1 ,padx = 5 ,pady = 5 )
e2.grid(row = 1 ,column = 1 ,padx = 5 ,pady = 5 )
def account():
num1 = str (e1.get())
num2 = str (e2.get())
print ( "两次输入数字的组合即将开始" )
list1 = [(x,y) for x in num1 for y in num2 ]
num3 = []
for list2 in list1:
print (''.join([ str (x) for x in list2]))
num3.append(''.join([ str (x) for x in list2]))
print ( "组合完成,共有" + str ( len (num3)) + "对组合!" )
Button(root,text = "进行组合" ,width = 10 ,command = account).grid(row = 3 ,column = 0 ,sticky = W,padx = 10 ,pady = 5 )
Button(root,text = "退出" ,width = 10 ,command = root.quit).grid(row = 3 ,column = 1 ,sticky = E,padx = 10 ,pady = 5 )
root.geometry( "300x100+200+20" )
mainloop() |
本文转自激情燃烧的岁月博客51CTO博客,原文链接http://blog.51cto.com/liuzhengwei521/1925499如需转载请自行联系原作者
weilovepan520