當前位置:成語大全網 - 新華字典 - 如何用python編寫壹個簡易的隨機點名軟件

如何用python編寫壹個簡易的隨機點名軟件

思路:1、定義壹個字典,把名字和數字對應起來;2、引用隨機模塊;3、輸入要點名的個數,通過循環輸出名字。

具體代碼如下:

#?-*-?coding:utf-8?-*-

from?random?import?randint

DictName?=?{1:?'Aaron',

2:?'Abel',

3:?'Abraham',

4:?'Adam',

5:?'Adrian',

6:?'Alva',

7:?'Alex',

8:?'Alexander',

9:?'Alan',

10:?'Albert',

11:?'Alfred',

12:?'Andrew',

13:?'Andy',

14:?'Angus',

15:?'Anthony',

16:?'Arthur',

17:?'Austin',

18:?'Ben',

19:?'Benson',

20:?'Bill',

21:?'Bob',

22:?'Brandon',

23:?'Brant',

24:?'Brent',

25:?'Brian',

26:?'Bruce',

27:?'Carl',

28:?'Cary',

29:?'Caspar',

30:?'Charles',

31:?'Cheney',

32:?'Chris',

33:?'Christian',

34:?'Christopher',

35:?'Colin',

36:?'Cosmo',

37:?'Daniel',

38:?'Dennis',

39:?'Derek',

40:?'Donald',

41:?'Douglas',

42:?'David',

43:?'Denny',

44:?'Edgar',

45:?'Edward',

46:?'Edwin',

47:?'Elliott',

48:?'Elvis',

49:?'Eric',

50:?'Evan',

51:?'Francis',

52:?'Frank',

53:?'Franklin',

54:?'Fred',

55:?'Gabriel',

56:?'Gaby',

57:?'Garfield',

58:?'Gary'}

Num?=?raw_input('請輸入需要點名個數:')

flag?=?True

while?flag:

if?Num.isdigit():

Ind?=?int(Num)

flag?=?False

else:

print?'輸入錯誤,請確認'

else:

for?i?in?range(Ind):

print?DictName[randint(1,58)]

運行結果:

請輸入需要點名個數:4

Brant

Cheney

David

Alan