Python 对分猜数游戏
Python Bisection Number Guessing Game
我正在尝试编写一个简单的二分法问题,只要我没有已注释掉的特定条件语句,它就可以正常工作。这是什么原因?这不是作业题。
low = 0
high = 100
ans = (low+high)/2
print "Please think of a number between 0 and 100!"
print "Is your secret number " + str(ans) + "?"
response = raw_input("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. ")
response = str(response)
while response != "c":
if response == "h":
high = ans
ans = (low + high)/2
print "Is your secret number " + str(ans) + "?"
response = raw_input("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. ")
response = str(response)
if response == "l":
low = ans
ans = (low + high)/2
print "Is your secret number " + str(ans) + "?"
response = raw_input("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. ")
response = str(response)
if response == "c" :
break
# if response != "c" or response != "h" or response != "l":
# response = raw_input("Please enter a 'h', 'l', or 'c' ")
# response = str(response)
print "Game over. Your secret number was: " + str(ans)
这是因为while循环和while循环的条件一样吗?如果是这样,改变这种情况的最佳方法是什么?
该条件将始终为真,因为您正在比较多个事物的不平等性。这就像问“如果这个字符不是 c
、 或 如果它不是 h
、 或 如果不是l
,做这个。不能同时是三个东西,所以它总是评估为真。
相反,您应该使用 if response not in ['c','h','l']
,这基本上就像将上面句子中的 or 替换为 and。或者在您的情况下更好,只需使用 else
语句,因为您现有的条件已经确保您要检查的内容。
我建议你使用双 while 循环:
while True:
response = None
print "Is your secret number " + str(ans) + "?"
response = str(raw_input("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."))
while response not in ['h', 'c', 'l']:
response = str(raw_input("Please enter a 'h', 'l', or 'c' "))
if response == 'c':
break
if response == 'l':
low = ans
else:
high = ans
ans = (low + high)/2
我正在尝试编写一个简单的二分法问题,只要我没有已注释掉的特定条件语句,它就可以正常工作。这是什么原因?这不是作业题。
low = 0
high = 100
ans = (low+high)/2
print "Please think of a number between 0 and 100!"
print "Is your secret number " + str(ans) + "?"
response = raw_input("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. ")
response = str(response)
while response != "c":
if response == "h":
high = ans
ans = (low + high)/2
print "Is your secret number " + str(ans) + "?"
response = raw_input("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. ")
response = str(response)
if response == "l":
low = ans
ans = (low + high)/2
print "Is your secret number " + str(ans) + "?"
response = raw_input("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. ")
response = str(response)
if response == "c" :
break
# if response != "c" or response != "h" or response != "l":
# response = raw_input("Please enter a 'h', 'l', or 'c' ")
# response = str(response)
print "Game over. Your secret number was: " + str(ans)
这是因为while循环和while循环的条件一样吗?如果是这样,改变这种情况的最佳方法是什么?
该条件将始终为真,因为您正在比较多个事物的不平等性。这就像问“如果这个字符不是 c
、 或 如果它不是 h
、 或 如果不是l
,做这个。不能同时是三个东西,所以它总是评估为真。
相反,您应该使用 if response not in ['c','h','l']
,这基本上就像将上面句子中的 or 替换为 and。或者在您的情况下更好,只需使用 else
语句,因为您现有的条件已经确保您要检查的内容。
我建议你使用双 while 循环:
while True:
response = None
print "Is your secret number " + str(ans) + "?"
response = str(raw_input("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."))
while response not in ['h', 'c', 'l']:
response = str(raw_input("Please enter a 'h', 'l', or 'c' "))
if response == 'c':
break
if response == 'l':
low = ans
else:
high = ans
ans = (low + high)/2