设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

猜密码游戏

[复制链接]

0

主题

0

好友

36

积分

新手上路

Rank: 1

楼主
发表于 2016-3-28 09:55:31 |显示全部楼层
  1. import random

  2. end = '4A0B'
  3. end_try = ''
  4. lst_password = []

  5. def creat_password():
  6.        
  7.         for i in range(4):
  8.                 x1 = random.randint(0,9)
  9.                 lst_password.append(x1)
  10.        
  11.         #print 'the password is: ' , lst_password
  12.         return lst_password
  13.        
  14. def compare():
  15.        
  16.         number_A = 0
  17.         number_B = 0
  18.         print 'Now, please guess the password!'
  19.         for i in range(0,4):
  20.                
  21.                 number = int(raw_input('Enter the number(0-9): '))
  22.                
  23.                 if number == lst_password[i]:
  24.                         number_A += 1
  25.                 else:       
  26.                         if number in lst_password:
  27.                                 number_B += 1
  28.                         else:
  29.                                 pass                       
  30.         end_try = '%dA%dB'        % (number_A,number_B)       
  31.         print end_try
  32.         return end_try

  33.        
  34. print 'Start Game!'
  35. creat_password()
  36. while end_try != '4A0B':
  37.         compare()
  38. print 'You got the right password.Congratulitions!'       
复制代码
C老师,绝对新手请教啊,为什么我这个代码,即使输出了4A0B,也结束不了呢,还是会继续让猜数字,而不会输出最后“You got the right password. Congratulitions!”
回复

使用道具 举报

0

主题

0

好友

36

积分

新手上路

Rank: 1

沙发
发表于 2016-3-29 12:41:33 |显示全部楼层
crossin先生 发表于 2016-3-28 17:34
函数内部的end_try和外面的变量end_try不是同一个,具体原因参见后面关于作用域的课程。
你现在的做法, ...

好的呢,我再继续研究一下,谢谢C老师指点了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-9 05:22 , Processed in 0.033729 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部