我是python的新手,并试图修改我发现的脚本,但无论我做什么它都不会工作。我试图让它允许我说一件事,如果输入1并说出其他如果输入2 ...请帮助:(这是代码
#Computer attempts to guess a number you choose between 1 and 100 in 10 tries
answer = 'yes'
print ("Please, think of a number between 1 and 200. I am about to try to guess it in 20 tries.")
while answer == "yes":
NumOfTry = 20
NumToGuess = 50
LimitLow = 1
LimitHigh = 200
while NumOfTry != 0:
try:
print ("I try: ",NumToGuess)
print ("Please type: 1 for my try is too high")
print (" 2 for my try is too low")
print (" 3 I guessed your number")
HumanAnswer = int (input("So did I guess right?"))
if 1 < HumanAnswer > 3:
print ("Please enter a valid answer. 1, 2 and 3 are the valid choice")
NumOfTry = NumOfTry + 1
if HumanAnswer == 1:
LimitHigh = NumToGuess
print ("Hmm, so your number is between ",LimitLow, "and ", LimitHigh)
NumOfTry = NumOfTry - 1
print (NumOfTry, "attempts left")
NumToGuess = int (((LimitHigh - LimitLow)/2) + LimitLow)
if NumToGuess <= LimitLow:
NumToGuess = NumToGuess + 1
if LimitHigh - LimitLow == 2:
NumToGuess = LimitLow + 1
elif HumanAnswer == 2:
LimitLow = NumToGuess
print ("Hmm, so your number is between ",LimitLow, "and ", LimitHigh)
NumOfTry = NumOfTry - 1
print (NumOfTry, "attempts left")
NumToGuess = int (((LimitHigh - LimitLow)/2) + LimitLow)
if NumToGuess <= LimitLow:
NumToGuess = NumToGuess + 1
if LimitHigh - LimitLow == 2:
NumToGuess = LimitLow + 1
elif HumanAnswer == 3:
print ("Woo hoo! I won")
NumOfTry = 0
answer = input ('Do you want to play again? (yes=1/no=2)')
if 'yes':
print ("ok")
if 'no':
print ("thanks for playing")
答案 0 :(得分:0)
修复最后一个块
编辑:
if answer == 'yes':
print ("ok")
if answer == 'no':
print ("thanks for playing")
您应该有一个except
阻止,但在这种情况下,您不需要try
答案 1 :(得分:0)
我认为这解决了这个问题:当你使用try:关键字时,你没有跟进除了:,除此之外,我测试了它并且工作正常。
answer = 'yes'
print ("Please, think of a number between 1 and 200. I am about to try to guess it in 20 tries.")
while answer == "yes":
NumOfTry = 20
NumToGuess = 50
LimitLow = 1
LimitHigh = 200
while NumOfTry != 0:
try:
print ("I try: ",NumToGuess)
print ("Please type: 1 for my try is too high")
print (" 2 for my try is too low")
print (" 3 I guessed your number")
HumanAnswer = int (input("So did I guess right?"))
if 1 < HumanAnswer > 3:
print ("Please enter a valid answer. 1, 2 and 3 are the valid choice")
NumOfTry = NumOfTry + 1
if HumanAnswer == 1:
LimitHigh = NumToGuess
print ("Hmm, so your number is between ",LimitLow, "and ", LimitHigh)
NumOfTry = NumOfTry - 1
print (NumOfTry, "attempts left")
NumToGuess = int (((LimitHigh - LimitLow)/2) + LimitLow)
if NumToGuess <= LimitLow:
NumToGuess = NumToGuess + 1
if LimitHigh - LimitLow == 2:
NumToGuess = LimitLow + 1
elif HumanAnswer == 2:
LimitLow = NumToGuess
print ("Hmm, so your number is between ",LimitLow, "and ", LimitHigh)
NumOfTry = NumOfTry - 1
print (NumOfTry, "attempts left")
NumToGuess = int (((LimitHigh - LimitLow)/2) + LimitLow)
if NumToGuess <= LimitLow:
NumToGuess = NumToGuess + 1
if LimitHigh - LimitLow == 2:
NumToGuess = LimitLow + 1
elif HumanAnswer == 3:
print ("Woo hoo! I won")
NumOfTry = 0
answer = input ('Do you want to play again? (yes=1/no=2)')
if 'yes':
print ("ok")
if 'no':
print ("thanks for playing")
except:
print("Out of tries!")
答案 2 :(得分:0)
由于许多错误导致代码无效。它需要完全重写。这是一个简洁的重写版本,没有错误!!!!基本上,我的观点是重写代码比修复它更快。
from random import choice
try:
your_answer = int(raw_input("Enter number:"))
except:
print "Not a number"
raise SystemExit # No mercy.
all_numbers = range(1,101)
for repeat in range(10):
print "Guess:", all_numbers.pop(choice(all_numbers))