这是我的代码,问题是我在while循环中遇到问题时遇到问题,当我运行程序时,它没有打印数字,它实际上只是打印了%d的行。我在这里做错了什么?
smallest = 1
largest = 100
answer = int(raw_input("Think of a number between 1 and 100"))
correct = 68
while (correct != 'yes'):
guess = (smallest+largest)/2
print guess
correct = raw_input("Is your answer equal to %d" + "? If not, then please press enter 's' if it's smaller or 'l' if larger.")%d(guess, )
if (correct=='l'):
smallest = guess + 1
elif (correct=='s'):
largest = guess - 1
else:
print ("Hurray, I got it!!")
Cancel_FC = raw_input("Press any key to close console")
答案 0 :(得分:0)
你有这套:
correct = raw_input("Is your answer equal to %d" + "? If not, then please press enter 's' if it's smaller or 'l' if larger.")%d(guess, )
请改为尝试:
correct = raw_input(("Is your answer equal to %d" + "? If not, then please press enter 's' if it's smaller or 'l' if larger.") % (guess))
此更改来自您的)%d(guess, )
到) % (guess)
答案 1 :(得分:0)
这样可行:
correct = raw_input( ("Is your answer equal to %d" + "? If not, then please press enter 's' if it's smaller or 'l' if larger.") % (guess) )
请注意附加的括号。
答案 2 :(得分:0)
正确的语法是:
correct = raw_input("Is your answer equal to %d? If not, then please press enter 's' if it's smaller or 'l' if larger."%(guess))
首先,格式化运算符只是%
,而不是%d
- 后者是你放在格式字符串中的内容。其次,运算符必须在格式字符串之后,而不是在raw_input
的参数列表之外;它对左侧的字符串和右侧的列表进行操作。
您不需要(guess)
内的逗号,但无论如何都可以。