首页 > 代码库 > python编程快速上手之第10章实践项目参考答案(10.8)

python编程快速上手之第10章实践项目参考答案(10.8)

 1 #通过DEBUG可以看出是guess输入内容(heads, tails)和TOSS内容(0,1)不一致,需修改和TOSS的比较项。
 2 import random,logging
 3 logging.basicConfig(level=logging.DEBUG, format=%(asctime)s - %(levelname)s - %(message)s)
 4 logging.debug(Start of program)
 5 guess = ‘‘
 6 n=‘‘                                         #新增一个函数n
 7 while guess not in (heads, tails):
 8   print(Guess the coin toss! Enter heads or tails:)
 9   guess = input()
10   logging.debug(inputwords:(%s%%) % (guess))#debug guess内容
11   if guess == (heads):
12     n = 1
13   elif guess == (tails):
14     n = 0
15   logging.debug(inputwords:(%s%%) % (n))#debug n 内容
16 toss = random.randint(0, 1) # 0 is tails, 1 is heads
17 logging.debug(toss:(%s%%) % (toss))  #debug toss 内容
18 if toss == n:
19   print(You got it!)
20 else:
21   print(Nope! Guess again!)
22   guesss = input()
23   if toss == n:
24     print(You got it!)
25   else:
26     print(Nope. You are really bad at this game.)

 

python编程快速上手之第10章实践项目参考答案(10.8)