(一)while循环
t = 2
while t <= 10:
print t
t += 2
print 'Goodbye!'
print 'Hello!'
t = 10
while t >= 2:
print t
t -= 2
t = 1
s = 0
while t <= end:
s += t
t += 1
print s
1. Convert the following code into code that uses a for loop. 将下列代码转换成使用for循环的代码。
print 2 print 4 print 6 print 8 print 10 print "Goodbye!"
t = 2
for x in range(5):
print t
t += 2
print 'Goodbye!'
2. Convert the following code into code that uses a for loop. 将下列代码转换成使用for循环的代码。
print "Hello!" print 10 print 8 print 6 print 4 print 2
t = 10
print 'Hello!'
for x in range(5):
print t
t -= 2
3. Write a for loop that sums the values 1 through end
, inclusive. end
is a variable that we define for you. So, for example, if we define end
to be 6, your code should print out the result:
编写一个从1加到end的for循环。变量end的值由我们给出。所以,举个例子,假如我们把end的值定为6,那么你的代码输出的结果应该是21,
21
which is 1 + 2 + 3 + 4 + 5 + 6. 也就是1+2+3+4+5+6的结果。
For problems such as these, do not include raw_input
statements or define the variable end
. Our automating testing will provide a value of end
for you - so write your code in the following box assuming end
is already defined.
对于这样的问题,请不要包含raw_input类型的语句或者给变量end赋值。我们的自动化测试将会帮你给end赋值-所以假定end已经被赋值,在下面的方框里写下你的代码。
以假定end已经被赋值,在下面的方框里写下你的代码。
s = 0
for x in range(1,end+1):
s += x
print s
The program works as follows: you (the user) thinks of an integer between 0 (inclusive) and 100 (not inclusive). The computer makes guesses, and you give it input - is its guess too high or too low? Using bisection search , the computer will guess the user's secret number!
Here is a transcript of an example session:
Please think of a number between 0 and 100! Is your secret number 50? 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 your secret number 75? 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 your secret number 87? 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 your secret number 81? 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 your secret number 84? 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 your secret number 82? 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 your secret number 83? 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 was: 83
Your program should use bisection search. So think carefully what that means. What will the first guess always be? How should you calculate subsequent guesses
Note: your program should be using raw_input
to obtain the user's input! Be sure to handle the case when the user's input is not one of h
, l
, or c
.
When the user enters something invalid, you should print out a message to the user explaining you did not understand their input. Then, you should re-ask the question, and prompt again for input. For example:
Is your secret number 91? 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. y Sorry, I did not understand your input. Is your secret number 91? 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
print('Please think of a number between 0 and 100!')
r = ''
high = 100
low = 0
mid = 0
while r != 'c':
mid = (high + low)/2
print('Is your secret number ' + str(mid)+ '?')
r = 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.'))
if r == 'h':
high = mid
elif r == 'l':
low = mid
elif r == 'c':
print('Game over. Your secret number was: ' + str(mid))
else:
print("Sorry, I did not understand your input.")