python编程快速上手 第10章实践项目答案

10.1投硬币游戏

一个简单的硬币投掷猜测游戏。玩家有两次猜的机会

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

猜你喜欢

转载自blog.csdn.net/u014221647/article/details/81017925
今日推荐