练习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
- 【SICP练习】1 练习1.1-练习1.5
- 练习1
- 练习1
- 练习1
- 练习1
- 练习。。。。。。1
- 练习1
- 练习1
- 练习1
- 练习1
- 练习1
- 练习1
- 练习1
- 练习1
- 练习1
- 练习1
- 练习1
- 练习1
- Unity热更新专题(三)Unity热更新之LuaInterface(上)
- 移动APP测试要点之性能、兼容、接口、交叉测试
- Nginx反向代理与负载均衡:节点服务器单/多虚拟机配置+实验环境搭建+原理解析
- java正则匹配中文姓名
- Bootstrap模态框遮罩问题
- 练习1
- 进程、线程相关的概念
- 蓝桥杯- 算法训练 Anagrams问题
- 我所理解的RxJava——上手其实很简单
- 大数据学习笔记之四十六 深度学习训练加速方法
- Android SDK “Connection to https://dl-ssl.google.com refused”
- Unity热更新专题(四)Unity热更新之LuaInterface(下)
- iOS代码规范(OC和Swift)
- (转)Android 手把手教您自定义ViewGroup(一)