我是 Python 新手。 这是我的代码,它实现了二进制搜索来查找猜测的数字。我无法正确地弄清楚如何使我的代码工作。 任何帮助,将不胜感激。谢谢。
print"Please think of a number between 0 and 100!"
guessed=False
while not guessed:
lo=0
hi=100
mid=(lo+hi)/2
print'Is you Secret Number'+str(mid)+'?'
print"Enter 'h' to indicate the guess is too high.",
print"Enter'l' to indicate the guess is too low",
print"Enter'c'to indicate I guessed correctly"
x=raw_input()
if(x=='c'):
guessed=True
elif(x=='h'):
#Too High Guess
lo=mid+1
elif(x=='l'):
lo=mid-1
else:
print("Sorry, I did not understand your input.")
print'Game Over','Your Secret Number was'+str()
最佳答案
以下几点需要申请代码:
- 在
for
循环之外定义下限和上限,因为如果我们在while 循环
内定义,每次lo
和hi
变量将分别创建0
和100
值。 - 根据变量的工作给出变量名。
较低 = 0
更高 = 100
上帝练习编写函数来包装您的代码。
因为猜测的数字更高 然后设置最大数字 来猜测数字。 由于猜测数较低 然后将最小数 设置为猜测数。
演示:
import time
def userNoInput(msg):
""" Get Number into from the user. """
while 1:
try:
return int(raw_input(msg))
except ValueError:
print "Enter Only Number string."
continue
def guessGame():
"""
1. Get Lower and Upeer Value number from the User.
2. time sleep to guess number for user in between range.
3. While infinite loop.
4. Get guess number from the Computer.
5. User can check guess number and tell computer that guess number if correct ror not.
6. If Correct then print msg and break While loop.
7. If not Correct then
Ask Computer will User that guess number is Greate or Lower then Actual number.
7.1. If Greater then Set Max limit as guess number.
7.2. If Not Greater then Set Min limit as guess number.
7.3. Continue While loop
"""
min_no = userNoInput("Please input the low number range:")
max_no = userNoInput("Please input the high number range:")
print "Guess any number between %d and %d."%(min_no, max_no)
time.sleep(2)
while True:
mid = (min_no+max_no)/2
print'Is you Secret Number'+str(mid)+'?'
print"Enter 'h' to indicate the guess is too high.",
print"Enter'l' to indicate the guess is too low",
print"Enter'c'to indicate I guessed correctly"
x=raw_input().lower()
if(x=='c'):
print'Game Over','Your Secret Number was'+str(mid)
break
elif(x=='h'):
#- As guess number is higher then set max number to guess number.
max_no=mid - 1
elif(x=='l'):
#- As guess number is lower then set min number to guess number.
min_no = mid + 1
else:
print("Sorry, I did not understand your input.")
guessGame()
输出:
vivek@vivek:~/Desktop/stackoverflow$ python guess_game.py
Please input the low number range:1
Please input the high number range:100
Guess any number between 1 and 100.
Is you Secret Number50?
Enter 'h' to indicate the guess is too high. Enter'l' to indicate the guess is too low Enter'c'to indicate I guessed correctly
h
Is you Secret Number25?
Enter 'h' to indicate the guess is too high. Enter'l' to indicate the guess is too low Enter'c'to indicate I guessed correctly
h
Is you Secret Number12?
Enter 'h' to indicate the guess is too high. Enter'l' to indicate the guess is too low Enter'c'to indicate I guessed correctly
l
Is you Secret Number18?
Enter 'h' to indicate the guess is too high. Enter'l' to indicate the guess is too low Enter'c'to indicate I guessed correctly
h
Is you Secret Number15?
Enter 'h' to indicate the guess is too high. Enter'l' to indicate the guess is too low Enter'c'to indicate I guessed correctly
l
Is you Secret Number16?
Enter 'h' to indicate the guess is too high. Enter'l' to indicate the guess is too low Enter'c'to indicate I guessed correctly
l
Is you Secret Number17?
Enter 'h' to indicate the guess is too high. Enter'l' to indicate the guess is too low Enter'c'to indicate I guessed correctly
c
Game Over Your Secret Number was17
关于python - 基本的 Python 代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30962747/