练习1

来源:互联网 发布:mac git客户端 编辑:程序博客网 时间:2024/06/06 00:44

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

from random import randintdef 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
原创粉丝点击