10.8 實(shí)踐項(xiàng)目:調(diào)試硬幣拋擲
項(xiàng)目要求:書中給了一段代碼蟆湖,隱藏了一些bug塔拳,用logging調(diào)試并找出問題所在
#! python3
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s,
%(levelname)s - %(message)s')
import random
def guess_fun():
global guess
while guess not in ('heads', 'tails'):
print('Guess the coin toss! Enter heads or tails:')
guess = input()
guess = ''
guess_fun()
toss = random.choice(['heads', 'tails']) # 0 is tails, 1 is heads
logging.info('toss is %s, guess is %s' % (toss, guess))
if toss == guess:
print('You got it!')
else:
print('Nope! Guess again!')
guess = ''
guess_fun()
logging.info('toss is %s, guess is %s' % (toss, guess))
if toss == guess:
print('You got it!')
else:
print('Nope. You are really bad at this game.')
思路:
- 基本上bug認(rèn)真讀讀代碼就可以看出來(lái)吁伺;但作為練習(xí)涛酗,還是要把logging插入到合適的位置
- logging作為調(diào)試陌兑,雖然用起來(lái)較麻煩沈跨,但是要習(xí)慣
環(huán)境:python3
想做這個(gè)系列文章,就是因?yàn)楫?dāng)時(shí)看這本書時(shí)兔综,想看看網(wǎng)上有沒更優(yōu)美的解決饿凛,但是略難找到。所以就把自己的項(xiàng)目練習(xí)放在了一個(gè)txt文件中软驰,現(xiàn)在把練習(xí)代碼放到這里涧窒,有不足之處希望大家能給出指導(dǎo)意見及相互交流、提升锭亏。