当前位置:嗨网首页>书籍在线阅读

23-调试硬币抛掷程序

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

调试硬币抛掷程序

下面程序实现一个简单的硬币抛掷猜测游戏。玩家有两次猜测机会(这是一个简单的游戏)。但是程序中有一些bug,让程序运行几次,找出bug,使该程序能正确运行:

import random 
guess = ''
while guess not in ('heads', 'tails'):
    print('Guess the coin toss! Enter heads or tails:') 
    guess = input()
toss = random.randint(0, 1) # 0 is tails, 1 is heads
if toss == guess:
    print('You got it!') 
else:
    print('Nope!  Guess   again!') 
    guesss  =  input()
    if toss == guess:
        print('You got it!')
    else:
        print('Nope. You are really bad at this game.')