Guess Number by Python
2010-10-22 14:14
357 查看
猜测一个随机数
#!/usr/bin/env python 'guessNumber.py -- my first Python : guess number' # import random function from random import randint # generate a random number number = randint(0, 100) # set default value counter = 0 last_num = { "min":0, "max":100, } # loop while True: n = int(raw_input("Enter a number between %d to %d:" % (last_num['min'], last_num['max']))) counter += 1 if not (last_num['min'] < n < last_num['max']): print "Invalid number. Please try again." # jump to next loop continue if n == number: print '%d times! And the number is %d' % (counter,number) raw_input('press anykey to exit') # finish, jump out of the loop break elif n < number: last_num['min'] = n elif n > number: last_num['max'] = n
#! /usr/bin/env python import sys,os import random def JudgeNum(num): print '**'*10 + '**'*10 print 'The random number is', num print 'if the random number is biger, please input [B]\n' print 'if the random number is lesser, please input [L]\n' print 'if the random number is equal, please input [E]\n' choiceFlag = raw_input('You choice is: ').upper() return choiceFlag #RandomNum = random.randint(1,100) #print 'The first random number is', RandomNum topNum = 100 lowNum = 1 counter = 0 while True: RandomNum = random.randint(lowNum,topNum) #print 'The first random number is', RandomNum choiceFlag = JudgeNum(RandomNum) if choiceFlag == 'B': topNum = RandomNum - 1 counter += 1 print 'topNum is: ', topNum print 'lowNum is: ', lowNum continue if choiceFlag == 'L': lowNum = RandomNum + 1 counter += 1 print 'topNum is: ', topNum print 'lowNum is: ', lowNum continue if choiceFlag == 'E': #print 'topNum is: ', topNum #print 'lowNum is: ', lowNum #counter += 1 print '%d times, guess right\n' %(counter) print 'The number ',RandomNum, ' is your want' break if choiceFlag != 'B' and choiceFlag != 'E' and choiceFlag != 'L': counter += 1 print 'Wrong input value, Please try it again......' #choiceFlag = raw_input('You choice is: ').upper() continue
相关文章推荐
- 【LEETCODE】374- Guess Number Higher or Lower [Python]
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 2 Guess the number
- Python [Leetcode 374]Guess Number Higher or Lower
- python Color By Number 图片生成像素图
- Python: Number of rows affected by cursor.execute("SELECT …)
- Interactive Python:Mini-project # 1 - "Guess the number" game"
- 莱斯大学Python课程Mini-project # 2 - "Guess the number" game
- [leetcode: Python]374. Guess Number Higher or Lower
- [LeetCode By Python]9. Palindrome Number
- [LeetCode By Python]171. Excel Sheet Column Number
- [LeetCode By Python] 2 Add Two Number
- Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project — “Guess the number” game
- 【LEETCODE】375- Guess Number Higher or Lower II [Python]
- Appium Python API 中文版 By-HZJ
- Guess Number Higher or Lower
- python groupby
- [leetcode: Python]17. Letter Combinations of a Phone Number
- 利用Oracle分析函数row_number和sys_connect_by_path实现多行数据合并为一行
- ROW_NUMBER() OVER(PARTITION BY)
- Scrapy爬虫系列笔记之二:搭建python以及其虚拟环境_by_书訢