Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

PyMathS1B 猜数字

Project: PyMathS1
Views: 93
Kernel: Python 3 (Anaconda 5)

猜数字问题

前言

猜一个 1~31 的数字,若没猜中,电脑会说太大或太小,此时使用者可以继续猜。请问要猜中至少要几次。

这问题是二进位相当经典的问题。在这专案,将了解二分法,与二进位的概念。此外,也利用 Python 的 input 指令,来达成一个互动式程式。

研究與探索

1 猜骰子所踯出的点数

1.a 用 random 隨機產生數

使用 random 的 randomint 来模拟丢一颗骰子的情况。

import random ans = random.randint(1,6) print(ans)
1

1.b 使用 input 來讓使用者輸入

让使用者输入一个数,再显示结果。

利用 input 取得使用者输入的数字

import random ans = random.randint(1,6) guess = input("Guess a number between 1 and 6: ") print("Answer {}. Guesss: {}".format(ans,guess))

1.c 让使用者反覆猜测直到猜中才结束

使用 while 回圈让使用者反覆输入

但在比對答案時要記住將資料轉為 int

import random ans = random.randint(1,6) guess = int(input("Guess a number between 1 and 6: ")) while(guess != ans): guess = int(input("Wrong! Guess again! ")) print("Bingo!!! The Answer is {}.".format(ans))

1.d 显示猜测的次数。

请修改上述程式,增加计数器 guessTime 。让猜对时,显示次数。

import random ans = random.randint(1,6) guess = int(input("Guess a number between 1 and 6: ")) guessTime = 1 while(guess != ans): guess = int(input("Wrong! Guess again! ")) guessTime += 1 print("Bingo!!! The Answer is {}. You used {} time(s).".format(ans,guessTime))

2 猜 1~31 内的数字,并显示太大或大小。

2.a 猜测后,判断太大或太小。

修正 1.d 猜骰子的程式,在 while 回圈内加入判断。 数字范围先设定在 1~31

import random ans = random.randint(1,31) guess = int(input("Guess a number between 1 and 31: ")) guessTime = 1 while(guess != ans): if guess < ans: guess = int(input("Too Small! Guess again! ")) else: guess = int(input("Too Large! Guess again! ")) guessTime += 1 print("Bingo!!! The Answer is {}. You used {} time(s).".format(ans,guessTime))

2.b 猜测后,更新可能范围

修正 2.a 的程式。新增两个变数 lower, upper 来纪录最大最小范围。

当猜测后,更新可能值的范围。

import random lower = 1 upper = 31 ans = random.randint(lower, upper) guess = int(input("Guess a number between {} and {}: ".format(lower,upper))) guessTime = 1 while(guess != ans): if guess < ans: lower = guess + 1 guess = int(input("Too small! Guess a number between {} and {}: ".format(lower,upper))) else: upper = guess - 1 guess = int(input("Too Large! Guess a number between {} and {}: ".format(lower,upper))) guessTime += 1 print("Bingo!!! The Answer is {}. You used {} time(s).".format(ans,guessTime))

2.c 错误输入的侦测

在上述程式,使用者若输入不在范围内的程式仍可执行。

请修正程式让程式提出紧告,只能显示在范围内的数。

import random lower = 1 upper = 31 ans = int(random.randint(lower, upper)) guess = int(input("Guess a number between {} and {}: ".format(lower,upper))) guessTime = 1 while(guess != ans): if lower <= guess and guess <= upper: if guess < ans: lower = guess + 1 guess = int(input("Too small! Guess a number between {} and {}: ".format(lower,upper))) else: upper = guess - 1 guess = int(input("Too Large! Guess a number between {} and {}: ".format(lower,upper))) guessTime += 1 else: guess = int(input("Error! Guess a number between {} and {}: ".format(lower,upper)))b print("Bingo!!! The Answer is {}. You used {} time(s).".format(ans,guessTime))

2.d 答案会动态调整

程式能动态给答案,让使用者用最多次输出

import random#設立一個random lower = 1 upper = 31 guess = int(input("Guess a number : ")) guessTime = 1 while(lower != upper): if guess-lower < upper-guess: lower = guess+1 guess = int(input("Too small! Guess a number between {} and {} : ".format(lower,upper))) else: upper = guess-1 guess = int(input("Too Large! Guess a number between {} and {} : ".format(lower,upper))) guessTime += 1 print("Bingo!!! The Answer is {}. You used {} time(s).".format(ans,guessTime))
Guess a number :

補充資料