练习1

今天看Learn Python The Hard Way,学习了EX35和EX36,然后自己写了一个小游戏,主要练习了函数和if语句。自己写的第一个小程序,不知道里面的英文用的是否正确。游戏还是有些boring,以后再改善。
附上程序代码:

from random import randint

def diamond_room():
    print("You're in a diamond room.")
    print("You can take the diamonds as many as you want to.")
    print("So, how many do you want to take? Please input a number.")
    diamond = int(input(">> "))
    if diamond <= 50:
        print("Congratulations! \nYou're a good person, and you can take %d diamonds for your own." %diamond)
        print("Game over.")
    elif diamond > 50:
        print("You're a greedy guy. You lose all the diamonds and fall into a bear room.")
        bear_room()
    else:
        print("I got no idea what that means.\nGame over.")

def bear_room():
    print("What are you going to do?")
    print("A. flee\nB. hit the bear\nC. paly dead ")
    choice = input(">> ")
    if choice.upper() == 'A':
        print("The bear chews your leg off.\nGame over.")
    elif choice.upper() == 'B':
        print("The bear slaps your face off.\nGame over.")
    elif choice.upper() == 'C':
        print("The bear goes away. You're alive.\nGame over.")
    else:
        print("I got no idea what that means.\nGame over.")

def guess_a_number():
    num = randint(1,10)
    print("Guess a number from 1 to 10.")
    bingo = False
    while bingo == False:
        guess = int(input(">> "))
        if guess < num:
            print("Too small!")
        elif guess > num:
            print("Too big!")
        else:
            print("Bingo! The diamond-room's door is open.")
            bingo = True
            diamond_room()


print("When you walk down the road, you suddenly fall into a hollow tree.")
print("There are two directions. Whice one do you chose? Right or Left?")
direction = input(">> ")
if direction.upper() == "LEFT":
    print("Well, you fall into a room which there are three buttons on the wall.")
    print("Please choose one button. Red? Green? or Yellow?")
    colour = input(">> ")
    if colour.upper() == 'RED':
        print("Bad luck! You fall into a bear room.")
        bear_room()
    elif colour.upper() == 'GREEN':
        print("You have to guess what I think.")
        guess_a_number()
    elif colour.upper() == 'YELLOW':
        diamond_room()
    else:
        print("I got no idea what that means.\nGame over.")
elif direction.upper() == 'RIGHT':
    print("Well, there are two bottles of water on the table. ")
    print("One smells good, another smells bad.")
    print("Please chose one to drink.")
    print("1. The good one\n2. The bad one")
    drink = input(">> ")
    if drink == '1':
        print("Bad luck! You fall into a bear room.")
        bear_room()
    elif drink == '2':
        diamond_room()
    else:
        print("I got no idea what that means.\nGame over.")
else:
    print("I got no idea what that means.\nGame over.")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值